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 4Name of the databaseParameter 5If the database server port is not filled in, the default is 3306
TypeDescription
Functionmysqli_connect
FunctionConnect to mysql database server
Parameter 1Host
Parameter 2Database server login name
Parameter 3Password
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

TypeExplanationFunctionmysqli_errnoFunctionReturns the connection error number, no error returns 0Parameter 1Pass in the resource returned by mysqli_connect
##TypeFunctionFunctionParameter 1Step 3: Select the database
Description
mysqli_error
Return connection error string
Pass in the resource returned by mysqli_connect

TypeFunctionFunctionParameter 1Parameter 2If 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

TypeFunctionFunctionParameter 1Parameter 2

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

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 1Pass in the resource returned by mysqli_connectParameter 2Pass in the SQL statement sent
TypeDescription
Functionmysqli_query
FunctionSend 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.

TypeDescription##FunctionFunctionParameter 1Parameter 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_assocGet 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
TypeDescriptionFunctionmysqli_fetch_row##FunctionGet the data in the result set and return the index array for convenienceParameter 1The result variable passed in from the queryType
Descriptionmysqli_fetch_object
##Function
FunctionGet the data in the result result set and return the object for traversal
Parameter 1Pass in the result variable from the query
##Functionmysqli_num_rowsFunctionReturn the total number of results from the queryParameter 1Pass in the result variable from the query
TypeDescription
TypeDescriptionFunctionmysqli_num_rowsFunctionReturn the total number of query resultsParameter 1Pass in the result variable from the queryNoteIt 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##Functionmysqli_fetch_fieldFunctionTraverse data rowsParameter 1Pass in the result variable from the queryModification 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

TypeDescriptionFunctionmysqli_closeFunctionClose the database connectionParameter 1Pass in the resource returned by mysqli_connectThe 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

TypeDescriptionFunctionmysqli_get_server_infoFunctionReturn server informationParameter 1Pass in the resource returned by mysqli_connect
##TypeDescriptionFunctionmysqli_get_server_version##FunctionReturn server versionParameter 1Pass in the resource returned by mysqli_connectNote: 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
||
<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } // some code ?>
submitReset Code