Php + mysqli use object-oriented method to query database instances, mysqli object-oriented _ PHP Tutorial

Source: Internet
Author: User
Php + mysqli queries database instances using object-oriented methods, and mysqli is object-oriented. Php + mysqli use the object-oriented method to query database instances. mysqli this article describes how php + mysqli uses the object-oriented method to query databases. I would like to share with you how to use php + mysqli to query database instances in an object-oriented way.

This article describes how php + mysqli queries databases using object-oriented methods. Share it with you for your reference. The specific implementation method is as follows:

<? Php // Step 1: Create a data connection object $ mysqli = new MySQLi ("localhost", "root", "123456"); // Default MySQL class, for its attributes and methods, see the manual if ($ mysqli-> connect_error) {// connect_error is the attribute, and the error die ("database connection failed :". $ mysqli-> connect_errno. "--". $ mysqli-> connect_error); // connect_errno: Error No.} $ mysqli-> select_db ("liuyan "); // select database $ mysqli-> query ("set names 'gbk'"); // Step 2: query the database. the code is as follows: $ SQL = "select title, contents from news limit 5, 5 "; // Step 3: Execute. The result $ res = $ mysqli-> query ($ SQL) or die ($ mysqli-> error) is displayed ); // return value $ res is the resource type (the resource type of the obtained result) if ($ res) {while ($ row = $ res-> fetch_assoc () {// note: fetch_assoc () is a function in MySQLi_Result. for details, see echo "title :". $ row ['title']."
Content :". $ row ['tents']. "" ;}}// Step 4: release the result set and close the connection $ res-> free (); $ mysqli-> close ();?>

I hope this article will help you with php programming.

Examples in this article describes how php + mysqli queries a database using object-oriented methods. Share it with you...

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.