Data access
1. Using the function is deprecated in the new version
2. Object-oriented approach Mysqli class
The way of 3.pdo
Using the Mysqli class to access the database
1. The Mysqli object is the equivalent of establishing a channel between PHP and MySQL database
$db = new Mysqli ("localhost", "root", "" "," 0812 ");
2. Determine if the link is wrong
if (Mysqli_connect_error ()) {
echo "Connection Failed";
Exit Terminate program directly if connection error occurs
}
3. Write SQL statements
$sql = "SELECT * from student";
4. Execute the prepared SQL statement
$result = $db->query ($sql); If a query statement is executed, the result set object is returned, and if the other statement is executed, returns true and False
5. Fetching data from the result set object
$arr = $result->fetch_row (); Read one at a time, return an array
Use while to read all data
while ($arr = $result->fetch_row ()) {
Var_dump ($arr);
//}
$arr = $result->fetch_all (); Read all returned two-dimensional arrays
Var_dump ($arr);
$arr = $result->fetch_assoc (); Reads a bar, returns an associative array
Var_dump ($arr);
$arr = $result->fetch_object (); Reads a bar, returns the object
Var_dump ($arr);
$arr = $result->fetch_all (); Read all returned two-dimensional arrays
Data Access for PHP