PHP operation MySQL Database 5 steps

Source: Internet
Author: User

PHP operation MySQL database generally can be divided into 5 steps: 1. Connect to the MySQL database server, 2. Select the database, 3. Execute the SQL statement; 4. Close the result set; 5 disconnect from the MySQL database server.

1. Connect the MySQL database server with the mysql_connect () function

Use the mysql_connect () function to establish a connection to the server. A different connection is then located based on the return value of this function.

"localhost" //mysql Server Address
"Root" //user name
"***" //Password
$connID = mysql_connect ($host, $user, $pwd); The connection identifier returned

2. Select the database file with the mysql_select_db () function

Use the mysql_select_db () function to select a database based on the connection identifier returned by the first step

mysql_select_db ($dbName, $connID); $dbName represents the database to select

3. Execute SQL statements with the mysql_query () function

First step: mysql_query ("SELECT * from Tb_stu", $connID); Executing a query returns a result set

The second step: Get the information from the result set above, there are two kinds of paths. 1. Use the Mysql_fetch_array () function to obtain information from the array result set; 2. Use the Mysql_fetch_object () function to get a row from the result set as an object. The difference is that the mysql_fetch_object () return value is an object, not an array, that is, the function can only access the array by word by.

$result = mysql_fetch_array($query);

OR

$result = mysql_fetch_object($query);

The operation of the data is generally the following 5 kinds:

1. Querying data (SELECT)

2. Display data (SELECT)

3. Inserting data (insert)

4. Updating data (update)

5. Deleting data (delete)

4. Close the result set

After the database operation is complete, you need to close the result set and release the resource

Mysql_free_result ($result);

5. Disconnecting the server

Each time the mysql_connect () or mysql_query () function is used, the system resources are consumed, in order to avoid wasting resources, use the mysql_close () The function closes the connection to the MySQL server to conserve system resources.

Mysql_close ($connID);

PHP operation MySQL Database 5 steps

Related Article

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.