PHP uses mysqli to connect to MySQL database

Source: Internet
Author: User

1. Object-oriented usage $db = new mysqli (' localhost ', ' root ', ' 123456 ', ' dbname '), select the database to use if the database is not specified when establishing the connection, switch the database used $db->select_ db (' dbname '); $query = "SELECT * from user WHERE uid=4"; $result = $db->query ($query); $result _num = $result->num_rows ; $row = $result->fetch_assoc ();    //returns an associative array that can be obtained by means of $row[' uid '] $row = $result->fetch_row ();    //Returns an enumeration array, which can be obtained by means of $row[0] $row = $result->fetch_array ();    //Returns a mixed array, which can be obtained by $row[' uid ' and $row[0] in two ways $row = $result->fetch_object ();    //Returns an object that can be $row->uid to get the value $result->free ();    //Release result set $db->close ();    //Closing a database connection is not necessary because the connection is closed automatically when the script is executed. When an INSERT, UPDATE, delete operation is performed, use $db->affected_ Rows view affects rows 2. Process-oriented usage $db = Mysqli_connect (' localhost ', ' root ', ' 123456 ', ' dbname '), and if the database is not specified when the connection is established, select the database to use. Switch the database used mysqli_select_db ($db, ' dbname '), query the database $query = "SELECT * from user WHERE uid=4"; $result = Mysqli_query ($db, $query ); $result _num = Mysqli_num_rows ($result); Returns a row of results $row = Mysqli_fetch_ASSOC ($result);    //returns an associative array that can be obtained by means of $row[' uid '] $row = Mysqli_fetch_row ($result);    //Returns an enumeration array, which can be obtained by $row[0] $row = Mysqli_fetch_array ($result);    //Returns a mixed array, which can be obtained by $row[' uid ' and $row[0] in two ways $row = Mysqli_fetch_object ($result);    //Returns an object that can be obtained by $ROW->UID to disconnect the database connection Mysqli_free_result ($result);    //Release result set Mysqli_close ($DB);    //shutting down a database connection is not necessary because the connection is closed automatically when the script is executed when an INSERT, UPDATE, delete operation is performed, the number of rows affected is viewed using mysqli_affected_rows ()

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.