MYSQLI provides two interfaces to operate the database MySQL
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