PHP Basics (13) using MYSQLI to operate MySQL database

Source: Internet
Author: User
Tags php basics stmt

MYSQLI provides two interfaces to operate the database MySQL

    1. Process oriented

2. Object-oriented

<?php/* process-oriented */$mysqli  = mysqli_connect ("localhost",  " Root ", " 123456 ", " MyDB ");if  (Mysqli_connect_errno ($mysqli))  {    echo   "failed to connect to mysql: "  . mysqli_connect_error ();} $res  = mysqli_query ($mysqli,  "SELECT&NBSP;*&NBSP;FROM&NBSP;MYTB"); $row  = mysqli_fetch _ASSOC ($res);echo  $row [' _msg '];/* object-oriented   recommends the use of the second */$mysqli  = new mysqli ("localhost",   "root",  "123456",  "MyDB");if  ($mysqli->connect_errno)  {     echo  "failed to connect to mysql: "  .  $mysqli->connect_error;} $res  =  $mysqli->query ("SELECT&NBSP;*&NBSP;FROM&NBSP;MYTB"); while ($row  =  $res->fetch_ Assoc ()) {    echo  $row ["id"]. "   ". $row [" Age "]."   ". $row [" name "]." \ n ";}?" 

For the sake of unification, the following are only explained using object-oriented methods.

MYSQLI Support Persistent Connection Persistent connection, persistent connection will not shut down after use, but put into the connection pool, if a request with the same hostname, password, port, database, then will continue to use connection pool connection, instead of re-create a new connection.


Mysqli->query (statements) caches the resulting data results in the client's memory for use.

mysqli-real_query("SELECT ID from test ORDER by ID ASC"); The result is still in MySQL Server to be read by the client, but not immediately cached to the clients, PHP consumes less memory, but the MySQL server is getting a lot of pressure.


The data that is queried by default is a string, unless setMYSQLI_OPT_INT_AND_FLOAT_NATIVE选项

$mysqli -> options ( ,&NBSP;

<?php$mysqli = new Mysqli ("localhost", "root", "123456", "MyDB"), $mysqli->options (mysqli_opt_int_and_float_ native,1);//Set auto-Convert if ($mysqli->connect_errno) {echo "Failed to connect to MySQL:". $mysqli->connect_error;} $res = $mysqli->query ("SELECT * from MYTB"), while ($row = $res->fetch_assoc ()) {echo $row ["id"].gettype ($row ["id"] );//Numeric type}


The MySQL database supports precompiled SQL statements so that SQL statements accept parameters and implement efficient execution of the same SQL statement.

The execution of a precompiled statement consists of two stages 1. precompilation 2. Bind parameters and Execute

<?php$mysqli = new Mysqli ("localhost", "root", "123456", "MyDB"), $mysqli->options (mysqli_opt_int_and_float_ native,1), if ($mysqli->connect_errno) {echo "Failed to connect to MySQL:". $mysqli->connect_error;} if ($stmt = $mysqli->prepare ("INSERT into MYTB (id,age,name) VALUES (?,?,?)")  {/* precompiled */$id = 4;  $age = 28;  $name = "shit"; $stmt->bind_param ("IIS", $id, $age, $name);/* bind parameter */}if ($stmt->execute ()) {/* Execute */echo "success!";}

Note: Unlike statements that are not precompiled, the result of the precompiled statement execution will automatically convert the data type to the correct type in the database (not all strings).


This article is from the "thick Product Thin Hair" blog, please make sure to keep this source http://joedlut.blog.51cto.com/6570198/1855943

PHP Basics (13) using mysqli to manipulate MySQL database

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.