Data access in an object-oriented manner

Source: Internet
Author: User

First, we need to build the object.

1 $db = new mysqli ("localhost", "root", "" "," AAAAA ");

The keyword is mysqli parentheses in the four parameters are ("To connect to the server IP address", "Server user name", "Password", "server name") to create the finished object can be handed to a variable $db, then this $db inside is a Mysqli object.

The second step is to determine if the connection is successful

1!mysqli_connect_error () or Die ("Connection failed! ");

If the connection succeeds Mysqli_connect_error () The return value is a true, the subsequent or die is not run ("Connection failed!"). ") statement, if the connection fails to return a false, it will continue to run or die (" Connection failed! ") statement, which shows--The connection failed!

The third step is to write the SQL statement

$sql = "SELECT * FROM News"; --Query all the data in the news table

Fourth step Execute SQL statement

1 $result = $db->query ($sql);

The Query method returns a result set object, and we need to build a variable to receive it.

The fifth step reads data from the result set object

1 $attr = $result->fetch_all ();

Here I use the Fetch_all method to extract data and Fetch_array, FETCH_ASSOC, Fetch_row, and so on, Fetvh_all returns an indexed array, received with $attr.


fetch_array--returns the data that the current pointer points to

fetvh_assoc--returns the data (associative array) to which the current pointer is pointing

fetch_object--returning objects

fetch_row--returning an indexed array

Data access in an object-oriented manner

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: 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.