PHP connects to the database to achieve the most basic addition, deletion, modification, and query (process-oriented)

Source: Internet
Author: User
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

12345678910111213141516

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:

123456789101112

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:

123456789101112

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
"; Echo" "; Echo" "; Echo" "; Echo" ";}?>
Id Nikename Email
". $ Row [0]."". $ Row [1]."". $ Row [2]."

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:

123456789101112

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.