Data access is divided into three ways:
1. Function mode (OBSOLETE);
2. Object-oriented approach;
3.PDO mode (data access abstraction layer);
Steps for data access in an object-oriented manner:
1. Create a Connection object
$db New Mysqli ("Connected address (native write localhost)", "username", "MySQL password", "called Database");
2. Determine if the current connection is correct
if (mysqli_connect_error()) { echo "Connection failed! "; Exit ;} // meet condition end, fail to meet conditional connection failure
3. Write an SQL statement
$s = "SELECT * from Xueshengbiao"; // The simplest SQL statement that queries all the contents of a table
4. Execute SQL statement If a result set object is returned successfully
$r $db->query ($s); // query is required. Specifies the SQL query to send.
5. Read query Results
(1) Fetch_all (): reads all data from the result object and returns a two-dimensional array;
(2) Fetch_array (): reads the data from the result set, each time one, returns a one-dimensional array, the index and the Association have;
(3) FECTH_ASSOC (): reads data from the result set, each time one, returns a one-dimensional array, only the associative array;
(4) Fecth_object (): reads data from the result set, each time one, returns a one-dimensional array, the equivalent of an object;
(5) Fecth_row (): reads data from the result set, each time one, returns a one-dimensional array, only an indexed array.
Cases:
if ($result) { // reads data from the result, reads one bar at a time, returns a one-dimensional Array (index) $arr$result Fetch_row (); // read a one-dimensional array at a time while ($arr$result,fetch_row ()) { var_dump($arr) ; }}
PHP Data Access