PHP database connection MySQL and mysqli contrast analysis _php skills

Source: Internet
Author: User
Tags php database php mysql

MySQL and the concept of mysqli related

1, MySQL and mysqli are PHP aspects of the set of functions , and the MySQL database is not associated.

2, in the PHP5 version, is generally used PHP MySQL function to drive the MySQL database, such as mysql_query () function, belong to the process 3, in PHP5 version after the addition of the MYSQLI function, in a sense, It is a MySQL system function of the enhanced version, more stable more efficient and more secure, and mysql_query () corresponding to the Mysqli_query (), belong to object-oriented, using the way of the object to drive MySQL database

Second, the difference between MySQL and mysqli

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

2, Mysqli is a permanent connection function, mysqli run more than once mysqli will use the same connection process, thereby reducing the cost of the server. Mysqli encapsulates some advanced operations, such as transactions, while encapsulating many of the methods available during DB operations.

Iii. MySQL and mysqli usage

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, specifies that 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 operate in a procedural way, some functions must specify resources, such as mysqli_query (resource identification, SQL statement), and resource identity parameters are placed in front, while mysql_query (SQL statement, ' resource ID ') is optional. The default value is the previous open connection or resource.

2, Mysqli (object mode):  

<pre name= "code" class= "php" > $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 one row of data 
 

Using the new mysqli (' localhost ', usenamer ', ' Password ', ' DatabaseName '), an error will be prompted as follows:

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

is generally mysqli is not open, because the Mysqli class is not the default to open, win under the change to PHP.ini, remove Php_mysqli.dll before, and Linux to the mysqli compiled.

Four, MySQL and mysqli examples

1, MySQL

<span style= "FONT-SIZE:14PX;" > $con =mysql_connect ($dbhostip, $username, $userpassword); 
$db = mysql_select_db ($dbdatabasename, $con); 
Executes 
the 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 query results 
//Extract multiple records 
$reslist = Array (); 
$i =0; 
while ($row = Mysql_fetch_row ($res)) { 
  $reslist [$i] = $row; 
  $i + +; 
 } 
Mysql_close ($con); 
</span> 

//mysql_fetch_row The result of the  extraction is that there is no field name in the query (i.e. no key id,goodsname, only value)



//MYSQL_FETCH_ASSOC The results of the extraction of the key value//mysql_fetch_array The result of the


key value, is the first two kinds of synthesis

Using the @ (Error control operator) before mysql_connect (), mysql_select_db (), and so on, you can ignore the error message from 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 return values (such as SELECT, Show, describe, and so on), the corresponding data is returned (when it succeeds) or false (when it fails), if the executed statement does not have a return value (such as Delete, DROP, INSERT, update, and so on), returns True (when it succeeds) or false (when it fails).

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 (); 

Examples of mysqli usage in PHP

<?php $variable = $_post[' variable ']; Post extracts the variable from the form if (! $variable)//If the variable is empty, output the error message and exit {echo ' Hava not entered search details. 
    Please go back and try again. '; 
  Exit The IF (!GET_MAGIC_QUOTES_GPC ())//The function is used to determine whether the GET_MAGIC_QUOTES_GPC is open, and the GET_MAGIC_QUOTES_GPC parameter is used to determine whether it will be from Post,get, Cookies over the data added escape characters and data from the database to remove the escape character {$variable = Addlashes ($variable);//Add escape characters before special text characters//stripslashes () used to remove escaped 
  character} $localhost = ' hostname ';//host name $user = ' username ';//username $pwd = ' password '; password $db = ' databasename '; @ $link = new Mysqli ($localhost, $user, $pwd, $db)//Connection database if (Mysqli_connect_errno ())//If database connection fails, output error message and exit {Ech O ' 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 results $num _results = $result-> num_rows; Result rows echo "<p>number of row found:". $num _results. " </p>/output line number for($i = 0; $i < $num _results $i + +)//loop output each group of elements {$row = $result-> Fetch_assoc ();//extract element, one row, Fetch_assoc () extract the element, 
  Property and Value Echo stripslashes ($row [' attributename ']);//Extract Value by property (key) echo "<br/>"; 
 $result-> Free ()//release memory $link-> close ();//Disconnect database connection?>

The above is about PHP database connection MySQL and mysqli differences and usage, I hope to help you learn.

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.