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

silencement
Release: 2023-02-24 19:44:01
Original
2087 people have browsed it

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);
Copy after login

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 />";
}
Copy after login

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 />";    
}
Copy after login

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 />";
}
Copy after login

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>">;
}
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template