PHP data access (for example, MySQL database)

Source: Internet
Author: User
Tags mysql injection

Build a connection, create a Connection object
$db = new Mysqli ("localhost", "root", "123", "MyDB");

Judging if there is an error
/*if (mysqli_connect_error ())
{
echo "Connection failed! ";
Exit
}*/

Mysqli_connect_error () Die ("Connection Failed"): "";

Write SQL statements
$sql = "SELECT * from Info";

Executes the SQL statement, returning the result set object
$reslut = $db->query ($sql);

reading data from the result set, returning an array
$attr = $reslut->fetch_all (); Reads all data, returns an indexed two-dimensional array

$attr = $reslut->fetch_array (); Reads the data that the current pointer points to, and returns an array that exists for the Index association

$attr = $reslut->fetch_assoc (); Returns an associative array

$attr = $reslut->fetch_row (); Returns an indexed array

$attr = $reslut->fetch_object (); Return object
/* $arr = array ();
while ($attr = $reslut->fetch_row ())
{
Array_push ($arr, $attr);
}

Var_dump ($arr); * *

Connecting objects
$db = new Mysqli ("localhost", "root", "123", "MyDB");

Write SQL statements
$sql = "INSERT into info values (' p003 ', ' Zhang San ', ' 1 ', ' n001 ', ' 1990-2-3 ')";

Execute SQL statement
$result = $db->query ($sql);

/*if ($result)
{
echo "executed successfully! ";
}
Else
{
echo "Failed to execute!" ";
}*/

$result->fetch_row ();

Beware of MySQL injection attacks

PHP data access (for example, MySQL database)

Related Article

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.