Add, delete, modify and query are the most basic and important in database operations.
After we have set up the PHP environment, let's take a look at how to connect to the MySQL database through PHP and how to implement the most basic additions, deletions, modifications and queries. Not much else to say, let’s go directly to the code.
Recommended related mysql video tutorials: "mysql tutorial"
1. Connect MySQL database with PHP
2. Add to the database Data
First of all, I created such a user table in the beyondweb_test database for demonstration. As shown in the figure:
SQL statement:
CREATE TABLE user( id INT(20) PRIMARY KEY AUTO_INCREMENT, nikename VARCHAR(30), email VARCHAR(40) );
Then add data to the database:
3. Modify the data in the database
We are also based on To operate the user table, for example, change "Zhang San" to "Li Si", the php code is as follows:
In fact, the SQL statement has changed, and the others are exactly the same.
4. Query the database
Just change the SQL statement. For example, query all the data in the table and output it in table form:
id | nikename | ".$row[0]." | "; echo"".$row[1]." | "; echo"".$row[2]." | "; echo""; } ?>
---|
5. Delete data
The solutions to adding data, modifying data, and deleting data have been given before. Then the only "delete" left in "add, delete, modify, and check" is "delete". Let's take a look at how to delete data. In fact, it is the same as "add, delete, modify, check". The above is almost the same, it only takes one sentence, just change the SQL statement. Let’s look at the PHP code:
Okay, the above is the PHP connection to the database and the most basic addition, deletion, modification and query operations. I hope it can be helpful to beginners.
But the above are all handled with a process-oriented approach, which is very complicated. If you are interested, you can follow the php Chinese website:
Related recommendations:
How to implement Mysql database connection, query, record set and other operations through PHP
How to set the MySQL time zone
The above is the detailed content of PHP connects to the database and implements the most basic addition, deletion, modification and query operations through a process-oriented approach.. For more information, please follow other related articles on the PHP Chinese website!