PHP uses mysqli extension to connect to the MySQL database, mysqlimysql

Source: Internet
Author: User

PHP uses mysqli extension to connect to the MySQL database, mysqlimysql

1. Object-oriented usage

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

If no database is specified during connection establishment, select the database to use and switch to the database to be 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 associated array, you can use $ row ['uid'] to obtain the value $ row = $ result-> fetch_row (); // return an array listing, you can get the value $ row = $ result-> fetch_array () through $ row [0]; // return a mixed array, you can use $ row ['uid'] or $ row [0] to obtain the value $ row = $ result-> fetch_object (); // return an object, you can get the value $ result-> free () through $ row-> uid; // release the result set $ db-> close (); // close a database connection, this is not necessary because the connection is automatically closed when the script is executed.

When performing INSERT, UPDATE, and DELETE operations, use $ db-> affected_rows to view the number of affected rows.

2. process-oriented usage

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

If no database is specified during connection establishment, select the database to use and switch to the database to be used.

mysqli_select_db($db, 'dbname');

Query a 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 associated array. You can obtain the value $ row = mysqli_fetch_row ($ result) by using $ row ['uid ); // returns a list array. You can obtain the value $ row = mysqli_fetch_array ($ result) by using $ row [0]; // returns a mixed array, you can use $ row ['uid'] or $ row [0] to obtain the value $ row = mysqli_fetch_object ($ result); // return an object, you can get the value by $ row-> uid.

Disconnect a database

Mysqli_free_result ($ result); // release the result set mysqli_close ($ db); // closes a database connection. This is not necessary because the connection is automatically closed when the script is executed.

When performing INSERT, UPDATE, and DELETE operations, use mysqli_affected_rows () to view the number of affected rows.


Why did I use the mysqli extension library to operate mysql databases?

For Encoding Problems, note that the encoding in the following three areas is uniform to ensure that Chinese characters are not garbled:
1. The Chinese encoding specified during Database Design (creation;
2. php file encoding;
3. encoding of connection settings after MYSQL connection, which is generally set using the following statement:
Mysql_query ("set names UTF-8 ");

Questions about database operations using the mysqli extension library of php

First, mysqli ("localhost", "root", "1234"); should be a function and should not use the new keyword.
New is used to instantiate objects.
$ Link = mysqli_connect (
'Localhost',/* The host to connect to The MySQL address */
'User',/* The user to connect as connection MySQL username */
'Password',/* The password to use to connect to The MySQL password */
'World ');
This is the connection statement.

Related Article

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.