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.