Home > Article > Backend Development > php calls mysql steps
php steps to call mysql: 1. Connect to the MySQL database; 2. Select the MySQL database; 3. Execute the SQL statement; 4. Close the result set; 5. Close the MySQL server.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
Five steps for PHP to access MYSQL database Detailed explanation (picture)
Database is necessary in our daily development of PHP, so MYSQL database is a database that many programmers love, because MYSQL is an open source and a little bit commercial. It has a relatively high market share, so it has always been considered the best partner of PHP. At the same time, PHP also has strong database support capabilities. This article mainly explains the basic steps for PHP to access the MySQL database.
The basic steps for PHP to access the MySQL database are as shown in the figure:
1. Connect to the MySQL database
Use the mysql_connect() function to establish Connection to MySQL server. Regarding the use of the mysql_connect() function, we will introduce it in detail later.
2. Select the MySQL database
Use the mysql_select_db() function to select the database of the MySQL database server. And establish a connection with the database. Regarding the use of the mysql_select_db() function, we will have a detailed explanation later.
3. Execute SQL statements
Use the mysql_query() function to execute SQL statements in the selected database. There are mainly 5 ways to operate the data. We will introduce them separately below. Query data: Use the select statement to implement the data query function.
Display data: Use the select statement to display the query results of the data.
Insert data: Use the insert into statement to insert data into the database.
Update data: Use the update statement to update records in the database.
Delete data: Use the delete statement to delete records in the database!
The specific use of the mysql_query() function will be introduced in detail later~
4. Close the result set
After the database operation is completed, the result set needs to be closed to release system resources. The syntax format is as follows: mysql_free_result($result);
Tips:
If database access is required frequently on multiple web pages, you can establish a continuous connection with the database server to improve efficiency. Because each connection to the database server takes a long time and requires a lot of resource overhead, a continuous connection is relatively more efficient. The way to establish a continuous connection is to call the function mysql_pconnect() instead of the mysql_connect function when the database is indirect. The established persistent connection does not need to call mysql_colse() to close the connection with the database server at the end of this program. The next time the program executes the mysql_pconnect() function here, the system will automatically directly return the established persistent connection ID number without actually connecting to the database.
5. Close the MySQL server
If the mysql_connect() or mysql_query() function is not used once, system resources will be consumed. Even if a small number of users complete the web site, the problem will not be big, but if the user When the number of connections exceeds a certain number, system performance will decrease or even crash. In order to avoid this phenomenon, after completing the database operation, you should use the mysql_close() function to close the connection with the MYSQL server to save system resources.
The syntax format is as follows: mysql_close($link);
Explanation:
The connection to the database in PHP is a non-persistent connection, and the system will automatically recycle it. Generally, there is no need to set it to close. However, if the one-time Fanhu result set is relatively large, or the website visits are high, it is best to use the mysql_close() function to release it manually.
The steps for PHP to access the MySQL database are over. Isn’t it very simple?
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of php calls mysql steps. For more information, please follow other related articles on the PHP Chinese website!