When PHP is connecting to the database in the following code, it uses the four major functions of mysql operations: add, delete, modify, and check. The source code is as follows:
Connect to the database
<?php $conn=mysql_connect('localhost','root',''); if(!$conn){ echo "connect failed"; exit; } $sql='use student'; mysql_query($sql,$conn);
Add
$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 />"; }
Delete
$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 />"; }
Modify
$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 />"; }
Query
$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>">; } ?>
The above is the detailed content of How to add, delete, modify and check mysql in php. For more information, please follow other related articles on the PHP Chinese website!