Home  >  Article  >  Backend Development  >  Add, delete, modify and query operations of PHP connection to Mysql

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

WBOY
WBOYOriginal
2016-08-08 09:31:581077browse

$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教程有兴趣的朋友有所帮助。

Statement:
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