Data Access for PHP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.