php操作mysql数据库的基本类代码
程序员文章站
2024-02-02 16:02:22
复制代码 代码如下:
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass='123456';
$dbname='products';
$connect=mysql_connect($dbhost,$dbuser,$dbpass);
if(!$connect) exit('数据库连接失败!');
mysql_select_db($dbname,$connect);
mysql_query('set names utf8');
//查询
$sql="select * from `category`";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
echo $row['id'];
}
//插入
$sql="insert into `category` (`id`,`name`) values (null,'".$name."')";
$result=mysql_query($sql);
if(mysql_affected_rows()){
echo '插入成功,插入id为:',mysql_insert_id();
}else{
echo '插入失败:',mysql_error();
}
//修改
$sql="update `category` set `name`='".$name."' where `id`='".$id."'";
$result=mysql_query($sql);
if(mysql_affected_rows()){
echo '修改成功!';
}
//删除
$sql="delete from `category` where `id`='".$id."'";
$result=mysql_query($sql);
if(mysql_affected_rows()){
echo '删除成功!';
}
//关闭连接
mysql_close($connect);
?>
复制代码 代码如下:
<?php
$dbhost='localhost';
$dbuser='root';
$dbpass='123456';
$dbname='products';
$connect=mysql_connect($dbhost,$dbuser,$dbpass);
if(!$connect) exit('数据库连接失败!');
mysql_select_db($dbname,$connect);
mysql_query('set names utf8');
//查询
$sql="select * from `category`";
$result=mysql_query($sql);
while($row=mysql_fetch_array($result)){
echo $row['id'];
}
//插入
$sql="insert into `category` (`id`,`name`) values (null,'".$name."')";
$result=mysql_query($sql);
if(mysql_affected_rows()){
echo '插入成功,插入id为:',mysql_insert_id();
}else{
echo '插入失败:',mysql_error();
}
//修改
$sql="update `category` set `name`='".$name."' where `id`='".$id."'";
$result=mysql_query($sql);
if(mysql_affected_rows()){
echo '修改成功!';
}
//删除
$sql="delete from `category` where `id`='".$id."'";
$result=mysql_query($sql);
if(mysql_affected_rows()){
echo '删除成功!';
}
//关闭连接
mysql_close($connect);
?>
上一篇: php获取bing每日壁纸示例分享
下一篇: 微机原理实验 实验二 存储器扩展实验