We have compiled the database connection as the most important 8 steps, I jokingly called it: "Database connection Tianlong eight steps."
These eight steps are described below, and the functions used in each step are explained:
First step: Connect to the database server
type |
Description |
Function |
Mysqli_connect |
Function |
Connecting to a MySQL database server |
Parameter 1 |
Host |
Parameter 2 |
Database Server login Name |
Parameter 3 |
Password |
Parameter 4 |
Name of the database |
Parameter 5 |
Database server port does not fill default 3306 |
If parameter 4, the database name is filled and selected in this step, no third step is required.
Step Two: Judging errors
type |
Description |
Function |
Mysqli_errno |
Function |
Return connection error number, no error returned 0 |
Parameter 1 |
Resources returned by incoming Mysqli_connect |
type |
Description |
Function |
Mysqli_error |
Function |
Returns the connection error string |
Parameter 1 |
Resources returned by incoming Mysqli_connect |
Step three: Select a database
type |
Description |
Function |
mysqli_select_db |
Function |
Select a database in this connection |
Parameter 1 |
Resources returned by incoming Mysqli_connect |
Parameter 2 |
The name of the database that needs to be connected |
If you have completed the first step of the database, you do not need to change to another database, you do not need to perform a third step.
Fourth step: Setting the character set
type |
Description |
Function |
Mysqli_set_charset |
Function |
Set the connection with MySQL power, result, check character set |
Parameter 1 |
Resources returned by incoming Mysqli_connect |
Parameter 2 |
Character set type |
For more attention, please follow this book "13.6 Data display garbled ultimate solution"
Fifth step: Preparing SQL statements
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 use variables in SQL statements to assign values. However, the variable or SQL statement error, very difficult to troubleshoot.
We have added this step in the light of actual work experience.
If you make an error while performing this step, we can print out the SQL statement and paste it into the phpMyAdmin or related tool.
If the execution succeeds, it is not an issue with the SQL statement. If execution fails, double-check the SQL statement.
Sixth step: Send SQL statements
type |
Description |
Function |
Mysqli_query |
Function |
Send SQL statement |
Parameter 1 |
Resources returned by incoming Mysqli_connect |
Parameter 2 |
Incoming sent SQL statement |
The SQL statement is ready to be completed and the SQL statement needs to be sent to the MySQL server via Mysqli_query.
The MySQL server executes the SQL statement sent over.
Seventh step: Determine whether to perform normal or traverse data read
In the 6th step, the statements for the Select category are sent, and the result output is usually displayed. You need to use a function that iterates through the display data.
type |
Description |
Function |
Mysqli_fetch_array |
Function |
Get data in result set, return array for convenience |
Parameter 1 |
Result variables passed in the query |
Parameter 2 |
Incoming Mysqli_num returns an indexed array, MYSQLI_ASSOC returns an associative array, Mysqli_both returns an index and an association |
type |
Description |
Function |
Mysqli_fetch_assoc |
Function |
Get data in result set, return associative array for convenience |
Parameter 1 |
Result variables passed in the query |
type |
Description |
Function |
Mysqli_fetch_row |
Function |
Get data in result set, return index array for convenience |
Parameter 1 |
Result variables passed in the query |
type |
Description |
Function |
Mysqli_fetch_object |
Function |
Get data in result set, return object for traversal |
Parameter 1 |
Result variables passed in the query |
type |
Description |
Function |
Mysqli_num_rows |
Function |
Returns the total number of results from the query |
Parameter 1 |
Result variables passed in the query |
type |
Description |
Function |
Mysqli_num_rows |
Function |
Returns the total number of results from the query |
Parameter 1 |
Result variables passed in the query |
Note |
Use very little in the actual work, understand |
Write
In the 6th step, if you send an INSERT statement, you usually need to get the success of the execution, or get the self-increment ID at the same time.
type |
Description |
Function |
Mysqli_fetch_field |
Function |
Traversing data rows |
Parameter 1 |
Result variables passed in the query |
Modify and delete
In the 6th step, if you are sending statements for the update and delete categories. You only need to determine if the execution succeeds.
We'll show you the data sheets for these common functions.
Eighth step: Close the database
type |
Description |
Function |
Mysqli_close |
Function |
To close a database connection |
Parameter 1 |
Resources returned by incoming Mysqli_connect |
A database connection is a resource type. We told you about the resource types in the previous chapters. Any type of resource that involves a number of resources is opened and closed. This ensures that PHP can process and recycle resources more efficiently.
Therefore, when the database connection succeeds, it does not need to be used. We can close this connection.
Other: Display server information functions
type |
Description |
Function |
Mysqli_get_server_info |
Function |
Return server information |
Parameter 1 |
Resources returned by incoming Mysqli_connect |
type |
Description |
Function |
Mysqli_get_server_version |
Function |
Back to server version |
Parameter 1 |
Resources returned by incoming Mysqli_connect |
Attention:
Mysqli only learn the process of the method can be. In the object-oriented phase, the object usage of mysqli is completely discarded, but the way that the PDO objects connect to the database is used.
Transferred from: http://www.php.cn/code/1254.html
PHP Eight steps to connect MySQL "real"