PHP Data Access

Source: Internet
Author: User

Data access is divided into three ways:

1. Function mode (OBSOLETE);

2. Object-oriented approach;

3.PDO mode (data access abstraction layer);

Steps for data access in an object-oriented manner:

1. Create a Connection object

$db New Mysqli ("Connected address (native write localhost)", "username", "MySQL password", "called Database");

2. Determine if the current connection is correct

if (mysqli_connect_error()) {    echo "Connection failed! ";     Exit ;}   // meet condition end, fail to meet conditional connection failure

3. Write an SQL statement

$s = "SELECT * from Xueshengbiao";  // The simplest SQL statement that queries all the contents of a table

4. Execute SQL statement If a result set object is returned successfully

$r $db->query ($s);  // query is required. Specifies the SQL query to send. 

5. Read query Results

(1) Fetch_all (): reads all data from the result object and returns a two-dimensional array;

(2) Fetch_array (): reads the data from the result set, each time one, returns a one-dimensional array, the index and the Association have;

(3) FECTH_ASSOC (): reads data from the result set, each time one, returns a one-dimensional array, only the associative array;

(4) Fecth_object (): reads data from the result set, each time one, returns a one-dimensional array, the equivalent of an object;

(5) Fecth_row (): reads data from the result set, each time one, returns a one-dimensional array, only an indexed array.

Cases:

if ($result) {    // reads data from the result, reads one bar at a time, returns a one-dimensional Array (index)    $arr$result Fetch_row ();         // read a one-dimensional array    at a time  while ($arr$result,fetch_row ()) {        var_dump($arr) ;    }}

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.