1. Use mysqli extension library in PHP to perform dql operations on MySQL
<? PHP Header ("Content-Type: text/html; charset = UTF-8" ); // Mysqli operations on the MySQL database (Object-Oriented) // 1. Create a mysqli object $ Mysqli = New Mysqli ("localhost", "root", "root", "test" ); If ( $ Mysqli -> Connect_error ){ Die ("Connection failed ".$ Mysqli -> Connect_error );} // 2. operate databases (send SQL statements) $ SQL = "Select * From user1" ; // 3. processing result $ Res = $ Mysqli -> Query ( $ SQL ); // Var_dump ($ res); // fetch_assoc \ fetch_array \ fetch_object While ( $ Row = $ Res -> Fetch_row ()){ Var_dump ( $ Row ); /* Foreach ($ row as $ Val) {echo '--'. $ val;} echo '<br/> '; */ } // 4. Close Resources $ Res ->Free (); $ Mysqli -> Close (); ?>
The following process-oriented
<? PHP Header ("Content-Type: text/html; charset = UTF-8" ); $ Mysqli = Mysqli_connect ("Localhost", "root", "root", "test" ); If (! $ Mysqli ){ Die ("Connection failed ". Mysqli_connect_error ());} $ SQL = "Select * From user1" ; $ Res = Mysqli_query ( $ Mysqli , $ SQL ); // Var_dump ($ res ); While ( $ Row = Mysqli_fetch_row ($ Res )){ Foreach ( $ Row As $ Val ){ Echo '-'. $ Val ;} Echo '<Br/>' ;} // Release memory Mysqli_free_result ($ Res ); Mysqli_close ( $ Mysqli ); ?>
2. Use the mysqli extension library in PHP to perform DML operations on MySQL
<? PHP // Crud operations on MySQL using mysqli extension Library Header ("Content-Type: text/html; charset = UTF-8" ); $ Mysqli = New Mysqli ("localhost", "root", "root", "test" ); If ( $ Mysqli -> Connect_error ){ Die ("Connection failed ". $ Mysql -> Connect_error );} // Add a record // $ SQL = "insert into user1 (name, password, email, age) values ('Lucy ', MD5 ('Lucy'), 'Lucy @ 163.com ', 17) "; // delete a record // $ SQL =" delete from user1 where id = 80 "; // update a record $ SQL = "Update user1 set age = 20 where id = 7" ; $ Res =$ Mysqli -> Query ( $ SQL ); If (! $ Res ){ Echo "Operation failed ". $ Mysqli -> Error ;} Else { If ( $ Mysqli -> Affected_rows> 0 ){ Echo "Successful" ;} Else { Echo "No rows affected" ;}} // Close Resources $ Mysqli -> Close (); ?>
3. Encapsulation
<? Class Sqlhelper { Private $ Mysqli ; // Write to death first, and then use a file for configuration. Private Static $ Host = "Localhost" ; Private Static $ User = "Root" ; Private Static $ Pwd = "Root" ; Private Static $ DB = "Test" ; Public Function _ Construct (){ $ This -> Mysqli = New Mysqli (SELF :: $ Host , Self :: $ User , Self :: $ Pwd , Self :: $ DB ); If ($ This -> Mysqli-> Connect_error ){ Die ("Connection failed ". $ This -> Mysqli-> Connect_error );} // Set Character Set $ This -> Mysqli-> query ("set names utf8" );} // Dql operate Function Execute_dql ( $ SQL ){ $ Res = $ This -> Mysqli-> query ( $ SQL ) Or Die ( $ This -> Mysqli-> Error ); Return $ Res ;} // DML operate Function Execute_dml ( $ SQL ){ $ Res = $ This -> Mysqli-> query ( $ SQL ) Or Die ( $ This -> Mysqli-> Error ); If (! $ Res ){ Return 0; // Failed }Else { If ( $ This -> Mysqli-> affected_rows> 0 ){ Return 1; // Successful } Else { Return 2; // No line impact }}}} ?>