Batch CRUD data of Mysqli, mysqlicrud
After learning to use the mysqli extension library, the overall feeling is more rational than the process-oriented method, object-oriented is the trend of the times, so we must not just use that old mysql library, it is not conducive to php operations on mysql, because many of us are still object-oriented in actual development. Here, let's talk about the reason why mysqli becomes an extension library, the batch operation feature greatly improves the code execution efficiency.
As in the past, I like to divide SQL statements into dql statements and dml statements, mainly based on the returned results. The former is the result set (to be released ), the latter is a Boolean value.
The code for batch operation of dml statements is as follows:
<? Php/* require_once "mysqltool. class. php "; $ SqlHelper = new SqlTool (); $ SQL =" insert into words (enword, chword) values ('classroom', 'classroom ')"; $ res = $ SqlHelper-> execute_dml ($ SQL); // $ res-> free (); * // This is mysqli batch add, delete, and modify data $ mysqli = new MySqli ("localhost", "root", "toor", "education "); if ($ mysqli-> connect_error) {die ("Connection Failed :". $ mysqli-> connect_error);} echo "connection successful <br/>";/* // Add data in batches $ SQL _1. = "insert into words (enword, chword) values ('C Lassroom444', 'classroom '); "; $ SQL _1. = "insert into words (enword, chword) values ('classroom222', 'classroom ');"; $ SQL _1. = "insert into words (enword, chword) values ('classroom333', 'classroom ');"; $ B = $ mysqli-> multi_query ($ SQL _1 ); * /// batch delete data/* $ SQL _2. = "delete from words where id = 14;"; $ SQL _2. = "delete from words where id = 15;"; $ SQL _2. = "delete from words where id = 16;"; $ SQL _2. = "delete from words where id = 17;"; $ SQL _2. = "delete from wor Ds where id = 18; "; $ SQL _2. = "delete from words where id = 19;"; $ SQL _2. = "delete from words where id = 20;"; $ SQL _2. = "delete from words where id = 22;"; * // You can also modify it in batches .... /* But it is not recommended to share with the select statement */$ B = $ mysqli-> multi_query ($ SQL _2); if (! $ B) {echo "failed to add";} else {echo "added successfully". $ mysqli-> connect_error;} $ mysqli-> close ();?>
The following is the code used to operate dql statements: it is a little complicated than dml:
<? Php $ mysqli = new MySqli ("localhost", "root", "toor", "education"); if ($ mysqli-> connect_error) {die ("Connection Failed: ". $ mysqli-> connect_error);} echo "connection successful <br/>"; <span style = "white-space: pre"> </span> $ SQL _3. = "select * from words;"; $ SQL _3. = "select * from words_test1;"; <span style = "white-space: pre"> </span> if ($ res = $ mysqli-> multi_query ($ SQL _3 )) {<span style = "white-space: pre"> </span> do {$ result = $ mysqli-> store_result (); while ( $ Row = $ result-> fetch_row () {foreach ($ row as $ key => $ val) {echo "----". $ val;} echo "<br/>" ;}$ result-> free (); if (! $ Mysqli-> more_results () {break ;} echo "************************************** *********";} while ($ mysqli-> next_result () ;}$ mysqli-> close () ;?>
This is mainly because the latter returns a set of result sets, which need to be stripped one by one to see what can be seen.
This article is only for reference and reprinted with csdn. Respect originality. Check my MyCodeDream blog and click to open the link.