After setting up the PHP environment, let's take a look at how to connect to the MySQL database through PHP and how to achieve the most basic addition, deletion, modification, and query. Let's not talk about anything else. just go to the code.
After setting up the PHP environment, let's take a look at how to connect to the MySQL database through PHP and how to achieve the most basic addition, deletion, modification, and query. Let's not talk about anything else. just go to the code.
1. connect PHP to the MySQL database
2. add data to the database
First, I created a user table in the beyondweb_test database for demonstration.
SQL statement:
12345 |
Create table user (id INT (20) primary key AUTO_INCREMENT, nikename VARCHAR (30), email VARCHAR (40 )); |
Then add data to the database:
3. modify data in the database
We also perform operations based on the user table. for example, we change "zhang san" to "Li Si". The php code is as follows:
In fact, the SQL statement has changed, and the others are the same.
4. query databases
You only need to replace the SQL statement, such as Querying all the data in the table and outputting it in the form of a table:
12345678910111213141516171819202122232425262728293031323334 |
Id |
Nikename |
Email |
"; Echo"
". $ Row [0]." | "; Echo"
". $ Row [1]." | "; Echo"
". $ Row [2]." | "; Echo"
";}?>
|
5. delete data
As we have already provided solutions for adding, modifying, and deleting data, "add, delete, modify, and query" will have one "delete" left, next, let's take a look at how data is deleted. In fact, it's similar to the above. just change an SQL statement in one sentence. Let's look at the PHP code:
Well, the above is the PHP connection to the database and the most basic addition, deletion, modification, and query operations, hoping to help beginners.
However, all of the above processes are process-oriented and complicated. if you are interested, refer to another article:
PHP connects to the database to achieve the most basic addition, deletion, modification, and query (object-oriented)