PHP database connection The difference and usage of MySQL and mysqli

Source: Internet
Author: User
Tags php database php mysql

First, MySQL and the concept of MYSQLI Related:

1, MySQL and mysqli are the function set of PHP, and the MySQL database is not very relevant.

2, before the PHP5 version, is generally used in PHP MySQL function to drive MySQL database, such as mysql_query () function, belongs to process 3, after the PHP5 version, added mysqli function function, in a sense, It is an enhanced version of MySQL system functions, more stable and more efficient and more secure, with mysql_query () corresponding to the Mysqli_query (), belongs to the object-oriented, the operation of the object to drive the MySQL database

Second, the difference between MySQL and mysqli:

1, MySQL is a non-holding connection function, MySQL each link will open a connected process.

2, Mysqli is the permanent connection function, mysqli multiple runs mysqli will use the same connection process, thereby reducing the server overhead. Mysqli encapsulates some of the advanced operations, such as transactions, while encapsulating many of the methods available in the DB operation process.

Iii. usage of MySQL and mysqli:

1:mysql (Process mode):

$conn =
mysql_connect (' localhost ', ' user ', ' password '); Connect MySQL database mysql_select_db (' data_base ');
Select Database $result =
mysql_query (' select * from Data_base ');//The Second optional parameter that specifies the open connection $row =
Mysql_fetch_row ($result))//Fetch only one row of data echo
$row [0]; Output the value of the first field

PS:MYSQLI operates in a procedural way, some functions must specify a resource, such as mysqli_query (resource ID, SQL statement), and the resource identity parameter is placed in front, and the resource identifier of the mysql_query (SQL statement, ' resource ID ') is optional, The default value is the previous open connection or resource.

2, Mysqli (object mode):

$conn = new
Mysqli (' localhost ', ' user ', ' password ', ' data_base '); To use the new operator, the last parameter is to specify the database directly//If the construction time is not specified, then the next sentence needs $conn-select_db (' data_base ') implementation $result =
$conn, Query (' SELECT * from Data_base '); $row =
$result-Fetch_row (); Fetch a row of data echo
ROW[0]; Output the value of the first field

Use new mysqli (' localhost ', usenamer ', ' Password ', ' databasename ');

Fatal error:class ' mysqli ' not found in ...

Generally mysqli is not open, because Mysqli class is not the default open, win under to change php.ini, remove php_mysqli.dll ago, Linux to put Mysqli in.

Iv. mysql_connect () and Mysqli_connect ()

1. Using mysqli, you can pass the database name as a parameter to the Mysqli_connect () function, or to the Mysqli constructor;

2. If you call Mysqli_query () or Mysqli's object query () method, the connection identity is required.

PHP database connection The difference and usage of MySQL and mysqli

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.