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