Home >Backend Development >PHP Problem >How to add, delete, modify and check mysql in php

How to add, delete, modify and check mysql in php

silencement
silencementOriginal
2019-09-25 10:06:222178browse

How to add, delete, modify and check mysql in php

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(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
if(!$conn){
echo "connect failed";
exit;
}
$sql=&#39;use student&#39;;
mysql_query($sql,$conn);

Add

$sql="insert into student(sname,sage) values(&#39;wx&#39;,&#39;18&#39;)";
$rs=mysql_query($sql,$conn);
if($rs){
    echo &#39;insert data success&#39;."<br />";
}else{
    echo &#39;insert data failed&#39;."<br />";
}

Delete

$sql="delete from student where sname=&#39;li&#39;";
$rs=mysql_query($sql,$conn);
if($rs){
    echo &#39;del data success&#39;."<br />";
}else {
    echo &#39;del data failed&#39;."<br />";    
}

Modify

$sql="update student set sname=&#39;fu&#39; where sname=&#39;pu&#39;";
$rs=mysql_query($sql,$conn);
if($rs){
    echo &#39;update data success&#39;."<br />";
}else{
    echo &#39;update data failed&#39;."<br />";
}

Query

$sql="select * from student";
$rs=mysql_query($sql,$conn);
var_dump($rs);
while($row=mysql_fetch_object($rs)){
print_r($row);
echo &#39;   &#39;;
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn