Comparison and analysis of connection between mysql and mysqli in the PHP database, mysqlmysqli_PHP tutorial

Source: Internet
Author: User
Tags mysql functions php database
Mysql and mysqli are connected to the PHP database. Mysql and mysqli are connected to the PHP database for comparative analysis. mysqlmysqli 1. concepts related to mysql and mysqli 1. mysql and mysqli are both function sets in php and are not associated with mysql databases. 2. Comparison and analysis of mysql and mysqli connected to the PHP database, mysqlmysqli

1. concepts related to mysql and mysqli

1. mysql and mysqli are both functions of php.Is not associated with the mysql database.

2. before php5, mysql functions in php are generally used to drive mysql databases. for example, mysql_query () is a process-oriented function. 3. after php5, the function of mysqli is added. in a sense, it is an enhanced version of mysql system functions, which is more stable, efficient, and secure. mysqli_query () corresponds to mysql_query (), it is an object-oriented, and uses an object-driven mysql database.

II. differences between mysql and mysqli

1. mysql does not support the connection function. a connection process is opened every time mysql is connected.

2. mysqli is a permanent connection function. if mysqli runs mysqli multiple times, the same connection process is used to reduce the overhead of the server. Mysqli encapsulates some advanced operations, such as transactions, and many available methods in the database operation process.

III. usage of mysql and mysqli

1. mysql (procedure ):

$ Conn = mysql_connect ('localhost', 'user', 'password'); // Connect to mysql database mysql_select_db ('data _ base '); // select database $ result = mysql_query ('select * from data_base '); // The Second Optional parameter, specifying the connection to be opened $ row = mysql_fetch_row ($ result )) // Obtain only one row of data echo $ row [0]; // output the value of the first field

PS: mysqli is operated in a procedural manner. some functions must specify resources, such as mysqli_query (resource ID, SQL statement), and the parameters of the resource ID are placed at the beginning, the resource ID of mysql_query (SQL statement, 'resource identified') is optional. the default value is the previous opened connection or resource.

2. mysqli (object method ):  

$ Conn = new mysqli ('localhost', 'user', 'password', 'data _ base'); // use the new operator, the last parameter is to directly specify the database // If this parameter is not specified during the construction, the next sentence requires $ conn-> select_db ('data _ base ') implement $ result = $ conn-> query ('select * from data_base '); $ row = $ result-> fetch_row (); // Retrieve a row of data echo row [0]; // output the value of the first field

When new mysqli ('localhost', usenamer ', 'password', 'databasename') is used, an error is returned, and the prompt is as follows:

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

In general, mysqli is not enabled, because the mysqli class is not enabled by default. in Windows, you need to change php. ini and remove php_mysqli.dll; in linux, you need to compile mysqli.

IV. Examples of mysql and mysqli

1. mysql

$ Con = mysql_connect ($ dbhostip, $ username, $ userpassword); $ db = mysql_select_db ($ dbdatabasename, $ con); // execute the statement $ qres = mysql_query ("SELECT id, goodsName FROM user "); // extract a piece of data $ row = mysql_fetch_row ($ result ); // mysql_fetch_row can only extract the first record of the query result // extract multiple records $ reslist = array (); $ I = 0; while ($ row = mysql_fetch_row ($ res )) {$ reslist [$ I] = $ row; $ I ++;} mysql_close ($ con ); // The result extracted by mysql_fetch_row is that no field name is in the query (that is, no key id, GoodsName, only value) // The extracted results of mysql_fetch_assoc include key values // The extracted results of mysql_fetch_array include key values, which are the synthesis of the previous two methods.

Use @ (error control operator) before functions such as mysql_connect () and mysql_select_db () to ignore system errors. then we use die () to customize error messages;
For the return value of the mysql_query () function, if the executed statement has a return value (such as SELECT, SHOW, DESCRIBE, etc.), the corresponding data (when successful) or FALSE (when failed) is returned ); if the executed statement does not return values (such as DELETE, DROP, INSERT, and UPDATE), TRUE (when successful) or FALSE (when failed) is returned ).

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");   $row=$result->fetch_row(); 

V. Examples of mysqli usage in php

<? Php $ variable = $ _ POST ['variable']; // Post extracts the variable if (! $ Variable) // if the variable is null, output the error message and exit {echo 'You hava not entered search details. Please go back and try again. '; exit;} if (! Get_magic_quotes_gpc () // This function is used to determine whether get_magic_quotes_gpc is enabled. the get_magic_quotes_gpc parameter is used to determine whether the request will be sent from post, get, escape characters are added to the data from cookies and escape characters from the database {$ variable = addlashes ($ variable ); // add the escape character before special text characters // stripslashes () is used to remove the escape character} $ localhost = 'hostname'; // host name $ user = 'username '; // username $ pwd = 'password'; // password $ db = 'databasename '; // @ $ link = new mysqli ($ localhost, $ user, $ pwd, $ db); // connect to the database if (mysqli_connect_errno () // if the database connection fails, output the Error message and exit {echo 'Error: Coulid not connect to database. please try again later. '; exit;} $ query = "SELECT row from table where some situation"; // query statement $ result = $ link-> query ($ query ); // query and return the result $ num_results = $ result-> num_rows; // The number of returned rows echo"

Number of row found: ". $ num_results ."

"; // The number of output rows for ($ I = 0; $ I <$ num_results; $ I ++) // cyclically output each group of elements {$ row = $ result-> fetch_assoc (); // extract elements, one row at a time, and the elements extracted by fetch_assoc, echo stripslashes ($ row ['butbutename']) with attributes and values; // extract values by attribute (key) echo"
";}$ Result-> free (); // release The Memory $ link-> close (); // disconnect the database?>

The above is about the difference and usage of connecting the PHP database to mysql and mysqli. I hope it will be helpful for your learning.

Articles you may be interested in:
  • Instructions on adding the extension = php_mysqli.dll command to php. ini
  • Analysis on the differences between mysql and mysqli in php
  • PHP Warning: PHP Startup: Unable to load dynamic library \ D:/php5/ext/php_mysqli.dll \
  • PHP5 mysqli prepare preparation statement instructions
  • How to connect to the database after mysqli extension is enabled in php
  • Solution to the lack of mysqli extension in phpmyadmin
  • Use instructions for PHP mysql and mysqli transactions
  • Php mysqli (sample code)
  • Mysqli-based database operation class library for PHP database operations

Concepts related to mysql and mysqli 1. mysql and mysqli are both functional sets in php and are not associated with mysql databases. 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.