Bulk crud data for mysqli

Source: Internet
Author: User

Learned to use the Mysqli extension library, the overall feeling than the process-oriented approach more reasonable, object-oriented is the trend, so must not just use that set of old MySQL library, also not conducive to PHP operation of MySQL, because we in the actual development, a lot of object-oriented development, Here, let's just say that the reason why Mysqli become an extended library is that the characteristics of bulk operation greatly improve the efficiency of code execution.


As always, I like to divide the SQL statements into DQL statements and DML statements, mainly based on the result of the return, which is the result set (which needs to be freed), and the latter is a Boolean value.


Here is the code for bulk operation of DML statements:

<?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 bulk additions and deletions data $mysqli= new Mysqli ("localhost", "root", "Toor", "education"), if ($mysqli->connect_error) {die ("Connection failed:". $mysqli->connect_ Error);} echo "Connection success <br/>";/*//bulk Add Data $sql_1.= "INSERT into words (Enword,chword) VALUES (' classroom444 ', ' classroom ');"; $sql _1.= "INSERT into words (Enword,chword) VALUES (' classroom222 ', ' classrooms ');"; $sql _1.= "INSERT into words (Enword,chword) VALUES (' classroom333 ', ' classrooms ');"; $b = $mysqli->multi_query ($sql _1); *///Bulk 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 words 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;"; *///can also be modified in bulk .... /* But not recommended for sharing with SELECT statements*/$b = $mysqli->multi_query ($sql _2), if (! $b) {echo "Add failed";} Else{echo "Add success". $mysqli->connect_error;} $mysqli->close ();? >


Here's the code for manipulating the DQL statement: It's a little more complicated than DML:

<?php$mysqli= new mysqli ("localhost", "root", "Toor", "education"), if ($mysqli->connect_error) {die ("Connection failed:". $ Mysqli->connect_error);} echo "Connection Success <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 ();? >

The main thing is that the latter returns a collection of result sets that need to be peeled off to see things.


This article is limited to reference and CSDN reprint, respect the original, view my Mycodedream personal blog Click to open the link



Bulk crud data for mysqli

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.