Bulk execution of SQL statements

Source: Internet
Author: User
Tags md5

    • Basic use

$SQLS = "SQL statement 1;sql statement 2;SQL statement n";

or $sqls = "insert into xx;"; $sqls. = "INSERT into xx;";

$res =mysqli::multi_query ($SQLS);

If $SQLS is a DML statement, $res returns a Boolean value;

If $SQLS is a DQL statement, $res returns multiple result sets, which requires the use of Mysqli::store_result ()

and Mysqli::more_results () Mysqli::next_result () to pull out each result set

    • Bulk execution of DML statements (INSERT, UPDATE, DELETE statements can be mixed)
1<?PHP2     //connecting to a database3     $mysqli=NewMysqli ("localhost", "root", "root", "test");4     //determine if the connection is successful5     if($mysqli-connect_error) {6          die("Connection failed.")$mysqli-connect_error);7     }8     //stitching The SQL statement, note that the semicolon in double quotes is not rare!!!9     $sqls= "INSERT into User1 (name,password,age,birthday) VALUES (' WPP ', MD5 (' WPP '), 25, ' 1991-05-05 ');";Ten     $sqls. = "INSERT into User1 (name,password,age,birthday) VALUES (' Zs ', MD5 (' ZS '), 26, ' 1990-01-01 ');"; One     $sqls. = "INSERT into User1 (name,password,age,birthday) VALUES (' ls ', MD5 (' ls '), 26, ' 1990-02-01 ');"; A     //batch execution of DML statements -     $res=$mysqli->multi_query ($sqls); -     //Judging Execution Results the     if(!$res){ -         Echo"Operation failed"; -}Else{ -         Echo"Operation succeeded"; +     } -     //Close Connection +     $mysqli-close (); A?>
    • Query the structure of the User1 table and the contents of the table and display
1<?PHP2     //connecting to a database3     $mysqli=NewMysqli ("localhost", "root", "root", "test");4     //determine if the connection is successful5     if($mysqli-connect_error) {6          die("Connection failed.")$mysqli-connect_error);7     }8     //stitching The SQL statement, note that the semicolon in double quotes is not rare!!!9     $sqls= "desc user1;";Ten     $sqls. = "SELECT * from User1"; One     //Bulk execution of DQL statements A     $res=$mysqli->multi_query ($sqls) or die("Operation failed".)$mysqli-error); -     //working with execution results -      Do{ the         Echo"<br/>-----results are as follows-----<br/>"; -         //Take out the first set of results -         $result=$mysqli-Store_result (); -         //Show Results +          while($row=$result-Fetch_row ()) { -             foreach($row  as $key=$val){ +                 Echo"--".$val; A             } at             Echo"<br/>"; -         } -         //release result set in time -         $result-Free ();  -} while($mysqli->more_results () &&$mysqli-Next_result ()); -     //Close Connection in     $mysqli-close (); -?>

The results are as follows:

    • Query The structure of the User1 table and the contents of the table and display it in a table
1<?PHP2     //Bulk Execute SQL statement (DQL), displayed in a tabular format on a Web page3 //Connect to database4     $mysqli=NewMysqli ("localhost", "root", "root", "test");5     //determine if the connection is successful6     if($mysqli-connect_error) {7          die("Connection failed.")$mysqli-connect_error);8     }9     //stitching The SQL statement, note that the semicolon in double quotes is not rare!!!Ten     $sqls= "desc user1;"; One     $sqls. = "SELECT * from User1"; A     //Bulk execution of DQL statements -     $res=$mysqli->multi_query ($sqls) or die("Operation failed".)$mysqli-error); -     //working with execution results the      Do{ -         Echo"<br/>-----results are as follows-----<br/>"; -         //Take out the first set of results -         $result=$mysqli-Store_result (); +         Echo"<table border=1 cellspacing=0 cellpadding=3px><tr>"; -         //display the first row of the header and table contents +         $fieldName=$result-Fetch_assoc (); A         foreach($fieldName  as $key=$val){ at             Echo"<th>$key</th> "; -         } -         Echo"</tr><tr>"; -         foreach($fieldName  as $key=$val){ -             Echo"<td>$val</td> "; -         } in         Echo"</tr>"; -         //Show the contents of the second row of the table to          while($row=$result-Fetch_row ()) { +             Echo"<tr>"; -             foreach($row  as $key=$val){ the                 Echo"<td>$val</td> "; *             } $             Echo"</tr>";Panax Notoginseng         } -         Echo"</table>"; the         //release result set in time +         $result-Free ();  A} while($mysqli->more_results () &&$mysqli-Next_result ()); the     //Close Connection +     $mysqli-close (); -?>

The results are as follows:

Bulk execution of SQL statements

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.