PHP database connection steps

We have organized the database connection into the eight most important steps for everyone. I jokingly call it: "The eight steps of database connection".

The eight steps are as follows, and the functions used in each step are explained:

Step one: Connect to the database server

##Parameter 4 Name of the database Parameter 5 If the database server port is not filled in, the default is 3306
Type Description
Function mysqli_connect
Function Connect to mysql database server
Parameter 1 Host
Parameter 2 Database server login name
Parameter 3 Password
If parameter 4, the database name is in this step Already filled in and selected, no need to perform the third step.

Step Two: Judgment Error

Type Explanation Function mysqli_errno Function Returns the connection error number, no error returns 0 Parameter 1 Pass in the resource returned by mysqli_connect
##Type Function Function Parameter 1 Step 3: Select the database
Description
mysqli_error
Return connection error string
Pass in the resource returned by mysqli_connect

Type Function Function Parameter 1 Parameter 2 If the database has been filled in in the first step, there is no need to change it to another database. Then there is no need to perform the third step.
Description
mysqli_select_db
Select the database in this connection
Pass in the resource returned by mysqli_connect
The name of the database to be connected

Step 4: Set character set

For more notes, please pay attention to the book "13.6 The Ultimate Solution to Garbled Data Display"

Step 5: Prepare the SQL statement

is actually a string of SQL statements.

For example:

We usually assign variables for use in SQL statements. However, there is an error in the variable or SQL statement, which is very difficult to troubleshoot.

We have added this step based on actual work experience.

If an error is reported when executing this step, we can print out the SQL statement and paste it into phpMyAdmin or related tools.

When debugging, if the execution is successful, it means that the problem is not with the SQL statement. If execution fails, double check the SQL statement.

Step 6: Send SQL statement

Type Function Function Parameter 1 Parameter 2
Description
mysqli_set_charset
Set the connection with the mysql server, result, verify character set
Pass in the resource returned by mysqli_connect
Character set type
##Parameter 1 Pass in the resource returned by mysqli_connect Parameter 2 Pass in the SQL statement sent
Type Description
Function mysqli_query
Function Send SQL statement
The SQL statement is prepared and needs to be sent to the MySQL server through mysqli_query.

The MySQL server will execute the SQL statement sent.

Step 7: Determine whether the execution is normal or traverse the data

Read

In step 6, the statement of the select category is sent, and the results usually need to be output and displayed. come out. You need to use the function that traverses the display data.

Type Description ##Function Function Parameter 1 Parameter 2
mysqli_fetch_array
Get the data in the result set and return the array for convenience
Pass Enter the result variable from the query
Pass in MYSQLI_NUM to return the index array, MYSQLI_ASSOC to return the associative array, MYSQLI_BOTH to return the index and association
Type ##Function mysqli_fetch_assoc Get the data in the result set and return the associative array for convenience Pass in the result variable from the query
Description
##Function
Parameter 1
Type Description Function mysqli_fetch_row ##Function Get the data in the result set and return the index array for convenience Parameter 1 The result variable passed in from the query Type
Description mysqli_fetch_object
##Function
Function Get the data in the result result set and return the object for traversal
Parameter 1 Pass in the result variable from the query
##Function mysqli_num_rows Function Return the total number of results from the query Parameter 1 Pass in the result variable from the query
Type Description
Type Description Function mysqli_num_rows Function Return the total number of query results Parameter 1 Pass in the result variable from the query Note It is rarely used in actual work, understand # #Write

In step 6, if the insert statement is sent, you usually need to get whether the execution is successful, or get the auto-incremented ID at the same time.

Type ##Function mysqli_fetch_field Function Traverse data rows Parameter 1 Pass in the result variable from the query Modification and deletion
Description
In step 6, if the statements of update and delete categories are sent. Just need to determine whether the execution is successful.

We list the data tables of these commonly used functions for everyone to check.

Step 8: Close the database

Type Description Function mysqli_close Function Close the database connection Parameter 1 Pass in the resource returned by mysqli_connect The database connection is a resource type. We told you about it when we explained resource types in the previous chapter. All resource types involved are either opened or closed. This ensures that PHP processes and recycles resources more efficiently.
Therefore, after the database connection is successful, there is no need to use it. We can close this connection.

Others: Display server information function

Type Description Function mysqli_get_server_info Function Return server information Parameter 1 Pass in the resource returned by mysqli_connect
##TypeContinuing Learning
||
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!
Description Function mysqli_get_server_version ##Function Return server version Parameter 1 Pass in the resource returned by mysqli_connect Note: mysqli only learns the procedural method. In the actual work of the object-oriented stage, the object usage of mysqli was completely abandoned, and instead the PDO object was used to connect to the database.