Connect to MySQL using the Mysqli library, which supports both object-oriented and process-oriented approaches:
1. How to use object-oriented
Establish a connection
$db = new mysqli (' localhost ', ' root ', ' 123456 ', ' dbname ');
Select the database to use if the database is not specified when the connection is established, switch the database used
$db->select_db (' dbname ');
Querying the database
$query = "SELECT * from user WHERE uid=4";
$result = $db->query ($query);
Statistics returns the number of rows in a record
$result _num = $result->num_rows;
Returns a row of results
$row = $result->fetch_assoc (); Returns an associative array that can be obtained by means of the $row[' UID ')
$row = $result->fetch_row (); Returns an array of enumerations, which can be obtained by means of $row[0]
$row = $result->fetch_array (); Returns a mixed array that can be obtained by $row[' uid '] and $row[0] two ways
$row = $result->fetch_object (); Returns an object that can be $row->uid to get a value
Disconnecting database connections
$result->free (); Releasing the result set
$db->close (); Shutting down a database connection is not necessary because the connection is closed automatically when the script finishes executing
Other than that:
Use $db->affected_rows to view the number of rows affected when an INSERT, UPDATE, delete operation occurs
2. Process-oriented usage
Establish a connection
$db = Mysqli_connect (' localhost ', ' root ', ' 123456 ', ' dbname ');
Select the database to use if the database is not specified when the connection is established, switch the database used
mysqli_select_db ($db, ' dbname ');
Querying the database
$query = "SELECT * from user WHERE uid=4";
$result = Mysqli_query ($db, $query);
Statistics returns the number of rows in a record
$result _num = mysqli_num_rows ($result);
Returns a row of results
$row = Mysqli_fetch_assoc ($result); Returns an associative array that can be obtained by means of the $row[' UID ')
$row = Mysqli_fetch_row ($result); Returns an array of enumerations, which can be obtained by means of $row[0]
$row = Mysqli_fetch_array ($result); Returns a mixed array that can be obtained by $row[' uid '] and $row[0] two ways
$row = Mysqli_fetch_object ($result); Returns an object that can be $row->uid to get a value
Disconnecting database connections
Mysqli_free_result ($result); Releasing the result set
Mysqli_close ($DB); Shutting down a database connection is not necessary because the connection is closed automatically when the script finishes executing
Other than that:
Use Mysqli_affected_rows () to view the number of rows affected when an INSERT, UPDATE, delete operation occurs