php操作mysql总结
时间:2021-07-01 10:21:17
帮助过:9人阅读
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);