Home >Backend Development >PHP Problem >How to use php to add, delete, modify and query mysql?
How to add, delete, modify and query PHP to operate mysql: 1. Insert statement [insert into data table name (field 1, field 2,....) values ("value 1", "value 2",. .)]; 2. Delete statement [delete from indicates where field = 'condition'].
How to add, delete, modify and query php to operate mysql:
1. Step 1: Go to www in wamp In the folder directory, create a test folder, and then create a message folder in the test folder to store each file that has been added, deleted, modified, and checked. (As shown in the figure)
2. Step 2: Remember to turn on wamp, and then create conn.php
in the www/test/message folder File, mainly used to connect php to mysql database.
The code is as follows: (You can also see the picture)
// 连接数据库:mysqli_connect(服务器名,用户名,密码,数据库名) $conn = mysqli_connect("localhost","root",'','user'); // 设置插入数据编码为utf8,不设置的话会出现乱码 mysqli_query($conn,"set names utf8");
3. Digression: Here I created a user database, you can also create what you need For databases, the key thing is that you need to know what the fields in your database look like.
SQL statement created by user database: (You only need to copy this part of the database code to the database statement to create this table. The first sentence is to create a database called user and create a message data table in it. .)
create database user; CREATE TABLE `message` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) CHARACTER SET utf8 DEFAULT NULL, `content` varchar(255) CHARACTER SET utf8 DEFAULT NULL, `who` varchar(255) CHARACTER SET utf8 DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
4. PHP operates Mysql to add data
The first step: Add data (add, delete, modify) During the search process, you must use the connection to the database)
First you have to create an index.php
in www/test/message/ to implement the operation php to add data to the mysql database. (You can create it as shown in the file path in the introduction)
Key place: Insert statement "insert into data table name (field 1, field 2,....) values ("value 1", "Value 2",....)"
<?php include "conn.php";//引用数据库连接文件 //关键的地方是sql语句的书写,一定不能写错,要对应前一个数据表的字段对应插入对应的值,然后实现插入数据。 $sql = "insert into message(title,content,who) values('留言标题','留言内容','留言人')"; $result = mysqli_query($conn,$sql);//处理数据语句,这句很重要!! if($result){ echo "发布留言成功!"; }else{ echo "发布留言失败!"; }
Note: When you want to run this part of the code, you need to turn on wamp, and then enter the path in the browser : localhost/test/message/index.php
, and then check it.
The following code runs in the same way!
5. PHP operates Mysql to delete data
Step 2: Implementation of adding data (process of adding, deleting, modifying and checking You must use the method to connect to the database)
First you have to create a shanchu.php
in www/test/message/ to implement the operation php to delete the data of the mysql database. (You can create it as shown in the file path in the introduction)
Key point: delete the statement "delete from indicates where field = 'condition' "
<?php include "conn.php";//引用数据库连接文件 // 删除留言,关键在于sql语句的改变 $sql = "delete from message where id='1'";//删除id=1的数据 $result = mysqli_query($conn,$sql);//这句是执行sql语句,很重要! if($result){ echo "删除成功!<a href='message.php'>返回</a>"; }else{ echo "删除失败!<a href='message.php'>返回</a>"; }
Note: You need to When running this part of the code, you need to turn on wamp, then enter the path :localhost/test/message/shanchu.php
in the browser, and then check it.
The following code runs in the same way!
6. PHP operates Mysql to realize data search
Step 3: Implementation of data search (process of adding, deleting, modifying and checking You must use the method to connect to the database)
First you have to create a message.php
in www/test/message/ to implement the operation PHP to delete the data of the mysql database. (You can create it as shown in the file path in the introduction)
Key place: Find the statement "select * from table name where condition (optional)"
Code part:
<?php include "conn.php";//引用数据库连接文件 $sql = "select * from message";//查询全部数据 $result = mysqli_query($conn,$sql); //处理sql语句 while($row = mysqli_fetch_array($result){ echo $row['title']; echo $row['content']; echo $row['who']; }
Note: When you want to run this part of the code, you need to enable wamp, then enter the path in the browser: localhost/test/message/message.php
, and then check of.
The following code runs in the same way!
7. PHP operates mysql to modify data
The third step: Implementation of modifying data (the process of adding, deleting, modifying and checking is Must be used to connect to the database)
First you have to create a xiugai.php
in www/test/message/ to implement the operation php to delete the data of the mysql database. (You can create it as shown in the file path in the introduction)
Key place: Modify the statement "update table name set field 1='value 1', field 2='value 2', field 3=' Value 3' where field='condition'"
Code part:
<?php include "conn.php";//引用数据库连接文件 // 修改对应id数据到数据库,修改id=1的数据 $sql="update message set title='修改标题',content='修改内容',who='修改名' where id='1'"; $result = mysqli_query($conn,$sql);//处理数据库 if($result){ echo "修改留言成功!"; }else{ echo "修改留言失败!"; }
注意:你要运行这部分代码的时候,是要开启wamp后,然后在浏览器里面路径输入:localhost/test/message/xiugai.php,然后查看的。
相关学习推荐:PHP编程从入门到精通
The above is the detailed content of How to use php to add, delete, modify and query mysql?. For more information, please follow other related articles on the PHP Chinese website!