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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.
