php操作mysql数据库的基本类代码_PHP

WBOY
Release: 2016-06-01 11:56:14
Original
783 people have browsed it
复制代码 代码如下:
$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);
?>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!