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