PHP access to the MYSQL database

Source: Internet
Author: User

1. Connect to the database

Use the mysql_connect () function to establish a connection with the MySQL database

Source code: $ con = mysql_connect ("host name or IP", "username", "password") or die ("cannot connect to the database, it may be that the database server is started or the user name and password are incorrect ". mysql_error ());

2. Select MySQL database

Use the mysql_select_db () function to select the database to use

Source code: $ db_selected = mysql_select_db ("Database Name", server connection ID) or die ("database selection failed". mysql_error ());

3. Execute SQL statements

In the selected database, use the mysql_query () function to execute the SQL statement.

[1] query statement: Use the select statement to query data

Source code: $ result = mysql_query ("SQL data query statement ");

[2] insert data: insert data to the database using the insert into statement

Data

Source code: $ result = mysql_query ("insert data SQL statement ");

[3] update data: Use the update statement to modify records in the database

Source code: $ result = mysql_query ("Change Data SQL statement ");

[4] delete data: Use the delete statement to delete records in the database

Source code: $ result = mysql_query ("delete data SQL statement ");

4. Disable the result set.

After the database operation is complete, you need to disable the result set to release system resources.

Mysql_free_result ($ result );

4. Close the database connection

Every time you use the mysql_connect () or mysql_query () function, system resources are consumed. If the number of user connections exceeds a certain number, the system performance may be degraded or even crashed. To avoid this problem, use the mysql_close () function to close the connection to the MYSQL server after database operations are completed to save system resources.

Mysql_close ($ con );

Note: The connection to the database in PHP is non-persistent, and the system will automatically recycle it. You do not need to disable it. However, if the one-time return result set is large, or the website has a large amount of traffic, you are advised to use the mysql_close () function for manual release.

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.