PHP database connection MySQL vs mysqli, mysqlmysqli_php tutorial

Source: Internet
Author: User
Tags php database php mysql

PHP database connection MySQL and mysqli comparative analysis, mysqlmysqli


First, MySQL and the concept of mysqli related

1, MySQL and mysqli are PHP's function set , and MySQL database is not associated with much.

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.

Third, MySQL and the use of 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, specifying 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 not specified at the time of construction, the next sentence needs $conn-select_db (' data_base ') implementation  $result = $conn, query (' SE  Lect * from Data_base ');  $row = $result fetch_row ();//Fetch one row of data  

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 and mysqli examples

1. mysql

$con =mysql_connect ($dbhostip, $username, $userpassword); $db = mysql_select_db ($dbdatabasename, $con);//EXECUTE statement $ Qres=mysql_query ("Select Id,goodsname from user"); Extracting a data $row =mysql_fetch_row ($result); Mysql_fetch_row can only extract the first record of the query results//extract multiple records $reslist = Array (); $i = 0; while ($row = Mysql_fetch_row ($res)) {   $reslist [$i] = $row;   $i + +  ; The result of mysql_fetch_row extraction is that there is  No field name in the query (that is, there is no key id,goodsname, only the value)//mysql_fetch_assoc The extracted result has a key value//mysql_fetch_ The result of an array extraction has a key value, which is a combination of the previous two

Using the @ (Error control operator) before functions such as mysql_connect (), mysql_select_db () can ignore the error message generated by the system, and then we use Die () to customize the error message;
For the return value of the mysql_query () function, if the executed statement has a return value (such as SELECT, SHOW, describe, and so on), the corresponding data is returned (on success) or FALSE (on failure), if the executed statement has no return value (such as Delete, DROP, INSERT, update, and so on), returns True (on success) or FALSE (on failure).

2, Mysqli

$db =new mysqli ($dbhostip, $username, $userpassword, $dbdatabasename);   if (Mysqli_connect_error ()) {    echo ' Could not connect to database. ';    Exit;   }   $result = $db->query ("Select Id,goodsname from user");   

V. Examples of mysqli usage in PHP

<?php $variable = $_post[' variable '); Post extracts variables from the form if (! $variable)//If the variable is empty, output an error message and exit {echo ' Hava not entered search details.     Go back and try again. ';   Exit if (!GET_MAGIC_QUOTES_GPC ())//This function is used to determine whether GET_MAGIC_QUOTES_GPC is turned on, the GET_MAGIC_QUOTES_GPC parameter is used for determining whether it will be from Post,get,   The cookie comes out of the data to add the escape character and the data from the database to remove the escape characters {$variable = Addlashes ($variable);//Add the escape character before the special text character//stripslashes () to remove the escape character  } $localhost = ' hostname ';//hostname $user = ' username ';//user name $pwd = ' password ';//password $db = ' databasename ';//@ $link = new Mysqli ($localhost, $user, $pwd, $db);//Connect database if (Mysqli_connect_errno ())//If database connection fails, output error message and exit {echo ' Error:co Ulid not connect to database.     Please try again later. ';   Exit } $query = "Select row from table where some situation";//query Statement $result = $link ($query);//Query and return results $num _re Sults = $result-num_rows; Result row number echo "

Number of row found: ". $num _results. "

";//output line number for ($i = 0; $i < $num _results; $i + +)//loop output each set of elements {$row = $result, Fetch_assoc ();//extract elements, one row, fetch _ASSOC () Extracts the element, has the property as well as the value echo stripslashes ($row [' attributename ']);//extract value by attribute (key) echo "
"; } $result free ();//Release Memory $link-close ();//Disconnect database connection?>

The above is about the PHP database connection between MySQL and mysqli the difference and usage, I hope that everyone's learning has helped.

Articles you may be interested in:

    • Instructions for adding a extension=php_mysqli.dll directive in php.ini
    • Some knowledge points about the differences between Mysqli and MySQL in PHP
    • PHP warning:php startup:unable to load dynamic library \ D:/php5/ext/php_mysqli.dll\
    • PHP5 MYSQLI's Prepare preparation statement usage instructions
    • How to connect to a database after PHP opens mysqli extension
    • Solutions to the lack of mysqli extension problems in phpMyAdmin
    • PHP MySQL and mysqli transaction usage instructions share
    • PHP Operation mysqli (sample code)
    • PHP database operation based on MYSQLI database Operation class Library

http://www.bkjia.com/PHPjc/1088791.html www.bkjia.com true http://www.bkjia.com/PHPjc/1088791.html techarticle PHP database connection mysql and mysqli comparative analysis, mysqlmysqli A, MySQL and mysqli concept related 1, MySQL and mysqli are the function set of PHP, and MySQL database is not associated with much. 2. In ...

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