PHP Data Access

Source: Internet
Author: User

1, PHP data access, is to access the database in PHP, to operate the MySQL database, must first establish a connection with the MySQL database.

Connecting objects
$db =new mysqli ("localhost", "root", "password", "database Name");

2, the establishment of a good link is to write SQL statements, through the statement to find the database:

Write SQL statements
$sql = "SELECT * from table name";
Execute SQL statement, return result set data
$result = $db->query ($sql);

3. Determine if there is any data

Determines whether null, 0 and Null are false, the general query first to determine whether there is data

if ($result->num_rows)
{

}

The following is whether it is empty or not, yes, it is wrong to read

if ($result)
{

}

4. Take data

$arr = $result->fetch_all (mysqli_both); //Use caution, return all arrays, both both indexes and associations

$arr = $result->fetch_array (); //read one bar at a time, both indexed and correlated

$arr = $result->fetch_all (); // returns all indexed arrays by default

$arr = $result->fetch_all (MYSQLI_ASSOC); //return associative array

$arr = $result->fetch_array (); //read one bar at a time, both indexed and correlated
while ($arr = $result->fetch_array ())//Use while loop to take all arrays
{
Var_dump ($arr);
}

$arr = $result->fetch_assoc (); //Returns an associative array each time
$arr = $result->fetch_object (); //Returns one object at a time
$arr = $result->fetch_row (); //Returns an indexed array at a time
while ($arr = $result->fetch_row ())
{
Var_dump ($arr); //Iterate through an array
}

Returns true if execution of additions and deletions is correct, and false for failure

$db = new Mysqli ("localhost", "root", "password", "table name");
$sql = "Delete from info where code= ' n011 '";
$result = $db->query ($sql);
Var_dump ($result);

PHP Data Access

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.