时间:2021-07-01 10:21:17 帮助过:3人阅读
以下代码时php在链接数据库,利用mysql操作的四大功能:增删改查,源码如下:
连接数据库
<?php $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('wx','18')"; $rs=mysql_query($sql,$conn); if($rs){ echo 'insert data success'."<br />"; }else{ echo 'insert data failed'."<br />"; }
删除
$sql="delete from student where sname='li'"; $rs=mysql_query($sql,$conn); if($rs){ echo 'del data success'."<br />"; }else { echo 'del data failed'."<br />"; }
修改
$sql="update student set sname='fu' where sname='pu'"; $rs=mysql_query($sql,$conn); if($rs){ echo 'update data success'."<br />"; }else{ echo 'update data failed'."<br />"; }
查询
$sql="select * from student"; $rs=mysql_query($sql,$conn); var_dump($rs); while($row=mysql_fetch_object($rs)){ print_r($row); echo ' '; echo $row->sname ."<br>">; } ?>
以上就是php如何对mysql进行增删修查的详细内容,更多请关注Gxl网其它相关文章!