Front-end link backstage, the database is almost necessary. So this article summarizes the common method steps of the PHP link database.
First of all
Link database: Mysqli_connect
Parameter ① host address ②mysql user name ③nysql password ④ Select the connected database ⑤ port number
Return: Returns the identifier of the resource type if the connection is successful
Connection failure returned false
If we have more than one connection to MySQL, then the various ha numbers in the future operations database must pass in the returned
Link symbol, if we have only one link to MySQL, then the various functions of database operation
You do not have to pass in this sign
Password is empty can omit password
$conn =mysqli_connect ("127.0.0.1", "root")
Secondly
To detect if a database connection is successful
Mysqli_connect_errno ([$conn]): Returns the error number of the last linked database. Link successfully returned 0
Mysqli_connect_error ([$conn]): Returns the error message for the last linked database
if (Mysqli_connect_errno ($conn)) {
Die ("Database connection failed, failure message". Mysqli_connect_error ($conn));
}
Linked Database simultaneous judgment
$conn =mysqli_connect ("127.0.0.1", "Root", "" "," zhuce ") or Die (" failed ");
And then
mysqli_select_db Selecting a Database
Parameter: ① Resource identifier ② the selected database name
Return: Link successfully returns TRUE, link failure returns false
If the modification data succeeds, the database in the resource identifier changes
If the modification fails without terminating the operation through the code, subsequent code can continue with the original database
mysqli_select_db ($conn, "zhuce") or Die ("Database selection failed");
Set the character set encoding format: Mysqli_set_charset ();
Can only be set to UTF8 instead of utf-8
Mysqli_set_charset ($conn, "UTF8") or Die ("coding failed");
Writing SQL statements
$sql = "SELECT * from Zhuce1";
Execute SQL statement
If it is increased, deleted, changed, will return the success of the Boolean type
If the query returns a resource result set
Failed to return false
$res =mysqli_query ($conn, $sql);
Returns the number of rows affected by the last manipulation
Var_dump (Mysqli_affected_rows ($conn));
Returns the last newly inserted primary key ID when executing the statement
Var_dump (mysqli_insert_id ($conn));
Var_dump ($res);
Var_dump (Mysqli_query ($conn, $sql));
Returns the number of rows in a resource result set when DQL
Var_dump (Mysqli_num_rows ($res));
Returns the number of fields in the resource result set when DQL
Var_dump (Mysqli_num_fields ($res));
Var_dump ($conn);
Working with result sets, returning associative arrays and indexed arrays
Parameter ① The result set to be processed
② returns that array format Mysql_assoc Association
Mysql_num Index
Mysql_both default, two arrays are produced at the same time
Var_dump (Mysqli_fetch_array ($res));
Var_dump (MYSQLI_FETCH_ASSOC ($res));//Correlation
Var_dump (Mysqli_fetch_object ($res));//Object
Mysqli_data_seek: Setting the result set pointer position
Mysqli_data_seek ($res, 0); Results reset to the very beginning.
Mysqli_data_seek ($res, 0);
Var_dump (Mysqli_fetch_object ($res));
At last
Returns the field information for each column in the result set. (field name, indicating, database name, field type, length, etc.) )
Var_dump (Mysqli_fetch_field ($res));
Free Query Resource result set
Mysqli_free_result ($res);
To close a database connection
Mysqli_close ($conn);
PHP connection Database (MySQL)