mysqli_multi_query
(PHP 5, PHP 7)
mysqli::multi_query -- mysqli_multi_query — 在数据库上执行查询。
说明
面向对象风格
bool mysqli::multi_query ( string $query )
过程化风格
bool mysqli_multi_query ( mysqli $link , string $query )
myqli_muti_query:执行由分号连接的一个或多个查询。
参数
link
仅以过程化样式:由 mysqli_connect() 或 mysqli_init() 返回的链接标识。
query
查询的字符串。
查询日期内escaped应该是恰当的。
返回值
如果第一个语句失败,返回false。检索后续错误你得先打电话给mysqli_next_result()其他报表。
案例:
面向对象风格
- <?php
- $mysqli = new mysqli ( "localhost" , "my_user" , "my_password" , "world" );
- /* check connection */
- if ( mysqli_connect_errno ()) {
- printf ( "Connect failed: %s\n" , mysqli_connect_error ());
- exit();
- }
- $query = "SELECT CURRENT_USER();" ;
- $query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5" ;
- /* execute multi query */
- if ( $mysqli -> multi_query ( $query )) {
- do {
- /* store first result set */
- if ( $result = $mysqli -> store_result ()) {
- while ( $row = $result -> fetch_row ()) {
- printf ( "%s\n" , $row [ 0 ]);
- }
- $result -> free ();
- }
- /* print divider */
- if ( $mysqli -> more_results ()) {
- printf ( "-----------------\n" );
- }
- } while ( $mysqli -> next_result ());
- }
- /* close connection */
- $mysqli -> close ();
- ?>
过程化风格
- <?php
- $link = mysqli_connect ( "localhost" , "my_user" , "my_password" , "world" );
- /* check connection */
- if ( mysqli_connect_errno ()) {
- printf ( "Connect failed: %s\n" , mysqli_connect_error ());
- exit();
- }
- $query = "SELECT CURRENT_USER();" ;
- $query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5" ;
- /* execute multi query */
- if ( mysqli_multi_query ( $link , $query )) {
- do {
- /* store first result set */
- if ( $result = mysqli_store_result ( $link )) {
- while ( $row = mysqli_fetch_row ( $result )) {
- printf ( "%s\n" , $row [ 0 ]);
- }
- mysqli_free_result ( $result );
- }
- /* print divider */
- if ( mysqli_more_results ( $link )) {
- printf ( "-----------------\n" );
- }
- } while ( mysqli_next_result ( $link ));
- }
- /* close connection */
- mysqli_close ( $link );
- ?>
以上例程的输出类似于:
- 以上例程的输出类似于:
- my_user@localhost
- -----------------
- Amersfoort
- Maastricht
- Dordrecht
- Leiden
- Haarlemmermeer
这个myqli_muti_query真的是很好用,可以一次执行多条SQL语句,当然这只是适用于PHP+mysql;而且还需要开启mysqli扩展;现在推荐大家用mysqli,因为它还支持mysqli预处理,防SQL注入的;
2017年06月16日 15:18:48 沙发
alert(‘付杰’);