欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php连接Mysql的增删改查操作

程序员文章站 2022-05-01 08:36:39
...
$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教程有兴趣的朋友有所帮助。