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
Type
Description
Function
mysqli_connect
Function
Connect to mysql database server
Parameter 1
Host
Parameter 2
Database server login name
Parameter 3
Password
##Parameter 4
Name of the database
Parameter 5
If the database server port is not filled in, the default is 3306
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
Description
Function
mysqli_error
Function
Return connection error string
Parameter 1
Pass in the resource returned by mysqli_connect
Step 3: Select the database
Type
Description
Function
mysqli_select_db
Function
Select the database in this connection
Parameter 1
Pass in the resource returned by mysqli_connect
Parameter 2
The name of the database to be connected
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.
Step 4: Set character set
Type
Description
Function
mysqli_set_charset
Function
Set the connection with the mysql server, result, verify character set
Parameter 1
Pass in the resource returned by mysqli_connect
Parameter 2
Character set type
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:
<?php
$sql = "insert into user(username,password) values('$username','$password')";
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
?>
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
Description
Function
mysqli_query
Function
Send SQL statement
##Parameter 1
Pass in the resource returned by mysqli_connect
Parameter 2
Pass in the SQL statement sent
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 dataReadIn 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
mysqli_fetch_array
Function
Get the data in the result set and return the array for convenience
Parameter 1
Pass Enter the result variable from the query
Parameter 2
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
Description
##Function
mysqli_fetch_assoc
##Function
Get the data in the result set and return the associative array for convenience
Parameter 1
Pass in the result variable from the query
Type
Description
Functionmysqli_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
##Function
mysqli_fetch_object
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
Type
Description
##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
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
Description
##Function
mysqli_fetch_field
Function
Traverse data rows
Parameter 1
Pass in the result variable from the query
Modification and deletion
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
##Type
Description
Functionmysqli_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.
Continuing Learning
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~
Students who have watched this course are also learning