PHP uses mysqli to connect to MySQL database

Source: Internet
Author: User
Connect to MySQL using the Mysqli library, which supports both object-oriented and process-oriented approaches:

1. How to use object-oriented

Establish a connection

$db = new mysqli (' localhost ', ' root ', ' 123456 ', ' dbname ');

Select the database to use if the database is not specified when the connection is established, switch the database used

$db->select_db (' dbname ');

Querying the database

$query = "SELECT * from user WHERE uid=4";

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

Statistics returns the number of rows in a record

$result _num = $result->num_rows;

Returns a row of results

$row = $result->fetch_assoc (); Returns an associative array that can be obtained by means of the $row[' UID ')

$row = $result->fetch_row (); Returns an array of enumerations, which can be obtained by means of $row[0]

$row = $result->fetch_array (); Returns a mixed array that can be obtained by $row[' uid '] and $row[0] two ways

$row = $result->fetch_object (); Returns an object that can be $row->uid to get a value

Disconnecting database connections

$result->free (); Releasing the result set

$db->close (); Shutting down a database connection is not necessary because the connection is closed automatically when the script finishes executing

Other than that:

Use $db->affected_rows to view the number of rows affected when an INSERT, UPDATE, delete operation occurs


2. Process-oriented usage

Establish a connection

$db = Mysqli_connect (' localhost ', ' root ', ' 123456 ', ' dbname ');

Select the database to use if the database is not specified when the connection is established, switch the database used

mysqli_select_db ($db, ' dbname ');

Querying the database

$query = "SELECT * from user WHERE uid=4";

$result = Mysqli_query ($db, $query);

Statistics returns the number of rows in a record

$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 the $row[' UID ')

$row = Mysqli_fetch_row ($result); Returns an array of enumerations, which can be obtained by means of $row[0]

$row = Mysqli_fetch_array ($result); Returns a mixed array that can be obtained by $row[' uid '] and $row[0] two ways

$row = Mysqli_fetch_object ($result); Returns an object that can be $row->uid to get a value

Disconnecting database connections

Mysqli_free_result ($result); Releasing the result set

Mysqli_close ($DB); Shutting down a database connection is not necessary because the connection is closed automatically when the script finishes executing

Other than that:

Use Mysqli_affected_rows () to view the number of rows affected when an INSERT, UPDATE, delete operation occurs

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