Php uses object-oriented interfaces to connect to the mysql database

Source: Internet
Author: User
This document describes how to connect php to the mysql database. It has a good reference value. let's take a look at it below. This article mainly introduces the methods and steps for connecting php to the mysql database. It has good reference value. let's take a look at it together with the small editor.

Connect to the database:

Use object-oriented methods;

1. create a mysqli object and create a connection object

2. prepare an SQL statement

3. execute the SQL statement, if it is a query statement. Returned results and objects

4. find data from the result set object;

Query property code:

 Query ($ SQL); // determines whether the returned content exists. if ($ reslut) is successfully executed {// if yes, data is found from the result set object; $ attr = $ reslut-> fetch_all (); // returns the index array: $ attr = $ reslut-> fetch_row () // returns the associated array: tch_assoc (); // returned object: $ attr = $ reslut-> fetch_object (); // returns both indexes and associations: $ arrt = $ reslut-> fetch_array (); var_dump ($ attr);?>

The final return value is a set Object. for the sake of clarity, we use fetch_all (), fetch_row (); fetch_object ();

Add, delete, and modify statements:

Add a message:

// Create a connection object $ db = new MySQLi ("localhost", "root", "12345678", "heiheihei "); // prepare the SQL statement $ SQL = "insert into cou values ('3-111 ', '', '123 ')"; // execute the SQL statement $ r = $ db-> query ($ SQL); if ($ r) // if $ r is true {echo "executed successfully ";} else {echo "execution failed ";}

Change one message:

 Query ($ SQL); if ($ r) {echo "execution successful" ;}else {echo "execution failed" ;}?>

Delete:

 Query ($ SQL); if ($ r) {echo "execution successful" ;}else {echo "execution failed" ;}?>

The above is the details of using object-oriented php to connect to the mysql database. For more information, see other related articles in the first PHP community!

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.