Mysql extension mysqli and mysqlmysqli

Source: Internet
Author: User
Tags php exception handling

Mysql extension mysqli and mysqlmysqli

Mysqli is the best and fastest mysql extension, and the only drawback is that it only applies to mysql operations.

First, check whether mysql has extended mysqli (using phpinfo () function output). Generally, versions above php5 support mysqli. Mysqli contains three important extension classes: mysqli, mysqli_result, mysqli_stmt

Use of the mysqli class:

To use mysqli to connect to a database, you must first use new mysqli

<Pre name = "code" class = "html"> <span style = "font-size: 14px;"> $ config = array ("host" => "localhost ", // host "user" => "root", // user "pwd" => "root", // password "dbname" => "test ", // Database "charset" => "utf8", // Character Set setting); $ mysqli = new mysqli ($ config ["host"], $ config ["user"], $ config ["pwd"], $ config ["dbname"]); // mysqli requires operations on a specific database </span>
 

In this statement, if the database is not enabled, the server will maintain a long waiting process. Therefore, you must ensure that mysql has been enabled during the operation.

<Span style = "font-size: 14px;"> if (mysqli_connect_errno () // use the mysqli_connent_errno () function to determine whether to connect to the database {echo "database link failed "; echo mysqli_connect_error (); // use mysqli_connect_error () to output a specific error.} </span>
Try {.. Throw ..} Catch (exception $ e. For details about php Exception Handling, refer to other articles in this blog. The function header uses error_reporting (0) to limit error prompts.

Note that it is best to set the character set when performing database operations.

<Span style = "font-size: 14px;"> $ mysqli-> set_charset ($ config ["charset"]); // echo "<br/> current link character status :". $ mysqli-> character_set_name (); </span>
Addition, deletion, query, and modification of databases are similar to traditional operations.

<Span style = "font-size: 14px;"> // $ SQL = "insert into user (id, name, sex) values ('1', 'irudder ', 'Man ') "; // Add // $ SQL =" update user set id = 2 where name = 'irudder '"; // modify // $ SQL = "delete from user where id = '2'"; // delete $ SQL = "select * from user"; // search </span>
Use $ mysqli-> query ($ SQL) to operate SQL statements; use $ mysqli-> errno to determine whether an error exists; use $ mysqli-> error to display an error


Some extensions of the mysqli class:

<Span style = "font-size: 14px;"> echo "Current mysql database version :". $ mysqli-> get_client_info (). "<br/>"; echo "current host information :". $ mysqli-> host_info. "<br/>"; echo "current server information :". $ mysqli-> get_server_info (). "<br/>"; echo "current server version :". $ mysqli-> server_version. "<br/>"; echo "what is the current SQL version :". $ mysqli-> protocol_version. "<br/>"; </span>

Use of the musqli_result class:

<Span style = "font-size: 14px;"> fetch_array-get the joined array. Both fields and numerical indexes (low efficiency): MYSQLI_NUM, MYSQLI_ASSOC, by default, MYSQLI_BOTHfetch_object-returns a data object. You can use the object-oriented calling method (generally not used, but the efficiency is moderate) fetch_row-to obtain the index array, which is highly efficient. fetch_assoc-to obtain the field array (Key array, high efficiency) query-return mysqli_result object # 1store_result-return mysql_result object # 2multi_query-return true if multiple SQL statements are executed, and false if an error is returned </span>
When multi_query is used for multiple SQL operations, once an error occurs, the subsequent statements are not executed, but no error message is prompted.


Use of the stmt class of mysqli:

Preprocessing Technology:

The client sends the statement to the database for preprocessing before returning the processed request.

$ Stmt = $ mysqli-> prepare ($ SQL); // pre-process data

$ Stmt> bind_param ('is', $ id, $ name); // I indicates the id type, and s indicates the name type. You can add multiple, bind a param to a specified database field. The ide and name can all be named.

<Span style = "font-size: 14px;"> $ SQL = "delete from user where id =? "; $ Stmt = $ mysqli-> prepare ($ SQL); // A preprocessing $ stmt-> bind_param ('I', $ id); $ id = 13; $ mysqli-> close (); // closes the extension of mysqli and releases the memory. </span>
$ Stmt: The store_result () function that contains a buffer zone: the client sends information to the database. After the database is processed, it is stored in the cache zone and used directly. If not, it is updated, the client actually receives the processing information of the buffer every time, which speeds up processing.
<Span style = "font-size: 14px;"> if ($ stmt-> store_result () // if the buffer contains my data, then I will get it back {$ stmt-> bind_result ($ id, $ a, $ B); while ($ stmt-> fetch ()) {echo "{$ id} ----- {$ a} ---- {$ B} <br/>" ;}}// echo "affected ". $ stmt-> affected_rows. "Line record! "; </Span>
<span style="font-size:14px;">........</span>
<span style="font-size:14px;">......</span>
<Span style = "font-size: 14px;"> $ stmt-> free_result (); // release the entire result set, data in the buffer $ mysqli-> close (); // closes the extension of the entire mysqli </span>
It is very important to release the database memory!








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.