当前位置:Gxlcms > PHP教程 > php连接Mysql的增删改查操作

php连接Mysql的增删改查操作

时间:2021-07-01 10:21:17 帮助过:5人阅读

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

人气教程排行