First, we need to build the object.
1 $db = new mysqli ("localhost", "root", "" "," AAAAA ");
The keyword is mysqli parentheses in the four parameters are ("To connect to the server IP address", "Server user name", "Password", "server name") to create the finished object can be handed to a variable $db, then this $db inside is a Mysqli object.
The second step is to determine if the connection is successful
1!mysqli_connect_error () or Die ("Connection failed! ");
If the connection succeeds Mysqli_connect_error () The return value is a true, the subsequent or die is not run ("Connection failed!"). ") statement, if the connection fails to return a false, it will continue to run or die (" Connection failed! ") statement, which shows--The connection failed!
The third step is to write the SQL statement
$sql = "SELECT * FROM News"; --Query all the data in the news table
Fourth step Execute SQL statement
1 $result = $db->query ($sql);
The Query method returns a result set object, and we need to build a variable to receive it.
The fifth step reads data from the result set object
1 $attr = $result->fetch_all ();
Here I use the Fetch_all method to extract data and Fetch_array, FETCH_ASSOC, Fetch_row, and so on, Fetvh_all returns an indexed array, received with $attr.
Report:
fetch_array--returns the data that the current pointer points to
fetvh_assoc--returns the data (associative array) to which the current pointer is pointing
fetch_object--returning objects
fetch_row--returning an indexed array
Data access in an object-oriented manner