Rumah > Artikel > pembangunan bahagian belakang > php如何对mysql进行增删修查
以下代码时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>">; } ?>
Atas ialah kandungan terperinci php如何对mysql进行增删修查. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!