PHP mysql\mysqli\pdo (ii) mysqli

Source: Internet
Author: User
Tags php mysql

Original link: http://www.orlion.ga/1147/

Mysqli has an object-oriented style and a process-oriented style, a personal feeling or an object-oriented style is better (after all, object-oriented)

1, Mysqli::_construct ()

mysqli::__construct  ([ string  $host  = ini_get (" Mysqli.default_host ")  [, string  $username  = ini_get (" Mysqli.default_user ")  [,  string  $passwd  = ini_get ("MYSQLI.DEFAULT_PW")  [, string$dbname =  ""  [, int  $port  = ini_get ("Mysqli.default_port")  [, string  $socket   = ini_get ("Mysqli.default_socket")  ]]]]]] ) mysqli mysqli_connect  ([ string  $host  = ini_get ("Mysqli.default_host")  [, string  $username = ini_get (" Mysqli.default_user ")  [, string  $passwd  = ini_get (" MYSQLI.DEFAULT_PW ")  [,string   $dbname  =  " [, int  $port  = ini_get (" Mysqli.default_port ")  [,  string  $socket  = ini_get ("Mysqli.default_socket")  ]]]]]] ) 

$host, $username, $passwd, $dbname, $port Needless to say, the rules are similar to MySQL.

2, Mysqli::close ()

Object-oriented style bool Mysqli::close (void)//Process style bool Mysqli_close (mysqli $link) returns TRUE on success, or FALSE on failure. $link: The link identity returned by Mysqli_connect () or mysqli_init (void), which provides a Mysqli object for use by Mysqli_options () and Mysqli_real_connect ().

The difference between this and mysql_close () is that mysql_close () can close the previous connection without providing parameters.

3, Mysqli::query ()

Mixed Mysqli::query (string $query [, int $resultmode = Mysqli_store_result]) mixed mysqli_query (mysqli $link, string $query [, int $resultmode = Mysqli_store_result])

$resultmode There are two ways to take a value:MYSQLI_USE_RESULTAndMYSQLI_STORE_RESULT(默认值),区别在于MYSQLI_USE_RESULT模式下取结果时每次都要从服务器上取结果而MYSQLI_STORE_RESULT则是将结果一步到位缓存到用户程序空间中。所以当查询结果数据量特别大时用MYSQLI_USE_RESULT,MYSQLI_STORE_RESUL则速度非常快。

return value:Return on FailureFALSEPassMysqli_query ()Successful executionSELECT, SHOW, DESCRIBEOrEXPLAINThe query returns a Mysqli_result object, other queries return true

4. Traverse the result function

Object-oriented style mixed mysqli_result::fetch_array ([int $resulttype = Mysqli_both])//process-oriented mixed Mysqli_fetch_array (mysqli_r Esult $result [, int $resulttype = Mysqli_both])

Cases:

 $query  =  "select name, countrycode from  City order by id limit 3 "; $result  =  $mysqli->query ($query);/*  numeric array */$row  =  $result->fetch_array (mysqli_num);p rintf  ("%s  (%s) \ n",   $row [0],  $row [1]);/* associative array */$row  =  $result->fetch_array ( MYSQLI_ASSOC);p rintf  ("%s  (%s) \ n",  $row ["Name"],  $row ["CountryCode"]);/* associative  and numeric array */$row  =  $result->fetch_array (mysqli_both);p rintf  ("% s  (%s) \ n ",  $row [0],  $row [" CountryCode "]); 

Similar functions are: Mysqli_result::fetch_assoc and Mysqli_result::fetch_row, Mysqli_result::fetch_object.

5. Release the result function

Object-oriented style void Mysqli_result::free (void) OID mysqli_result::close (void) void Mysqli_result::free_result (void)//process-oriented style void Mysqli_free_result (Mysqli_result $result)

6. Other functions

Mysqli::real_escape_string (string $sql) escapes SQL.

Executing multiple SQL at a time can use the Multi_query () method of the Mysqli object:

BOOL Mysqli::multi_query (String $query)

When passing parameters, multiple SQL commands need to be written in the same string as arguments to Multi_query (), and multiple SQL uses semicolons (;) delimited. If the first SQL command executes without error, this method returns True, otherwise false is returned.

Because each SQL in the Multi_query parameter may return a result, the process becomes:

if  ($mysqli->multi_query ($sql))  {    do {         if  ($result  =  $mysqli->store_result ())  {             while  ($row  =  $result->fetch_row ())  {                 foreach  ( $row  as  $data)  {                     var_dump ($data);                 }             }             $result->close ();         }        if  ($ Mysqli->more_results ())  {            echo  '----------------';         }    } while ($mysqli->next_result ());}

PHP mysql\mysqli\pdo (ii) 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.