以下程式碼時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進行增刪修查的詳細內容。更多資訊請關注PHP中文網其他相關文章!