The 1,mysqli extension Library allows us to access the MySQL database and perform curd operations on the MySQL database. mysqli extension cubby MySQL extension library has been enhanced.
Comparison of 2,mysqli extension libraries and MySQL extension libraries
The stability and safety of ①mysqli, the efficiency is improved;
②mysqli supports object-oriented programming, while the mysqli extension library takes into account the old PHP programmer, providing a process-oriented programming style.
3,mysqli has two sets of programming styles
4,mysqli Programming QuickStart, writing a program that reads data from the User1 data sheet and prints it in a Web page.
① Object-oriented implementation
<?php//mysqli operation MySQL Database (object oriented style)//1, create mysqli object $mysqli = new Mysqli ("127.0.0.1", "root", "123456", "test");// Verify if Okif ($mysqli->connect_error) {die ("Connection failed! ". $mysqli->connect_error);} 2, Operation database (send sql) $sql = "SELECT * from User1";//$res is the result set. mysqli result$res = $mysqli->query ($sql);//3, processing result while ($row = $res->fetch_row ()) { foreach ($row as $key = + $val) { echo "--$val"; } echo "<br/>";} 4, close the resource//release resource $res->free ();//close connection $mysqli->close ();
② Process-oriented implementation
<?php//mysqli operation MySQL database (for process style)//1, get mysqli connection $mysqli = Mysqli_connect ("127.0.0.1", "root", "123456", "test"); if ( ! $mysqli) {die ("Connection Failed". Mysqli_connect_error ($mysqli));} 2, send SQL statements to the database (Ddl,dml dql ..... ) $sql = "SELECT * from User1"; Mysqli_query ($mysqli, $sql);//3, processed results//loop out $res data Mysqli_fetch_row Mysql_fetch_ Rowwhile ($row =mysqli_fetch_row ($res)) { foreach ($row as $key + $val) { echo "--$val"; } echo "<br/>";} 4, close the resource//release resource Mysqli_free_result ($res);//close connection mysqli_close ();
5, the $res in the program is used to represent the execution result of the SQL statement
① If a DML statement is executed, BOOL is returned;
② If the DQL statement is executed, the query result mysqli_result result set object is returned;
6, the result of fetching the query from MYSQLI result has the following way:
①mysqli_result::fetch_assoc
②mysqli_result::fetch_row
③mysqli_result::fetch_array
④mysqli_result::fetch_object
PHP Database Programming---mysqli extension library