欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php操作mysql总结

程序员文章站 2022-04-04 14:25:52
...
01 02
$dbhost='localhost';
03
$dbuser='root';
04
$dbpass='123456';
05
$dbname='products';
06
$connect=mysql_connect($dbhost,$dbuser,$dbpass);
07
if(!$connect) exit('数据库连接失败!');
08
mysql_select_db($dbname,$connect);
09
mysql_query('set names utf8');
10
//查询
11
$sql="SELECT * FROM `category`";
12
$result=mysql_query($sql);
13
while($row=mysql_fetch_array($result)){
14
echo $row['id'];
15
}
16
//插入
17
$sql="INSERT INTO `category` (`id`,`name`) VALUES (NULL,'".$name."')";
18
$result=mysql_query($sql);
19
if(mysql_affected_rows()){
20
echo '插入成功,插入ID为:',mysql_insert_id();
21
}else{
22
echo '插入失败:',mysql_error();
23
}
24
//修改
25
$sql="UPDATE `category` SET `name`='".$name."' WHERE `id`='".$id."'";
26
$result=mysql_query($sql);
27
if(mysql_affected_rows()){
28
echo '修改成功!';
29
}
30
//删除
31
$sql="DELETE FROM `category` WHERE `id`='".$id."'";
32
$result=mysql_query($sql);
33
if(mysql_affected_rows()){
34
echo '删除成功!';
35
}
36
//关闭连接
37
mysql_close($connect);
相关标签: php操作mysql总结