Add, delete, modify and query operations of PHP connection to Mysql

WBOY
Release: 2016-08-08 09:31:58
Original
1032 people have browsed it

$conn=mysql_connect('localhost','root','');  
if(!$conn){  
echo "connect failed";  
exit;  
}  
  
$sql='use student';
mysql_query($sql,$conn);
  
//增加  
$sql="insert into student(sname,sage) values('pu','20')";  
$rs=mysql_query($sql,$conn);  
if($rs){  
    echo 'insert data success'."
";  
}else{  
    echo 'insert data failed'."
";  
}  
  
//修改  
$sql="update student set sname='fu' where sname='pu'";  
$rs=mysql_query($sql,$conn);  
if($rs){  
    echo 'update data success'."
";  
}else{  
    echo 'update data failed'."
";  
}  
  
//删除  
$sql="delete from student where sname='li'";  
$rs=mysql_query($sql,$conn);  
if($rs){  
    echo 'del data success'."
";  
}else {  
    echo 'del data failed'."
";    
}  
  
//查询  
$sql="select * from student";  
$rs=mysql_query($sql,$conn);  
//打印变量的相关信息  
var_dump($rs);  
//遍历从结果集中取行 mysql_fetch_array/assoc/row/object  
while($row=mysql_fetch_object($rs)){  
print_r($row);  
echo '   ';  
echo $row->sname;  
echo "
";  
}  
?>

以上就介绍了php连接Mysql的增删改查操作,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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!