mysqli批量執行多條語句和一次函數調用執行多條語句方法

來源:互聯網
上載者:User
本文主要和大家分享mysqli批量執行多條語句和一次函數調用執行多條語句方法,希望大家通過本文的執行個體能有自己的思路。

支援在單個字串中指定的多語句的執行。要想與給定的串連一起使用該功能,開啟串連時,必須將標誌參數中的CLIENT_MULTI_STATEMENTS選項指定給mysql_real_connect()。也可以通過調用mysql_set_server_option(MYSQL_OPTION_MULTI_STATEMENTS_ON),為已有的串連設定它。

常用套路:

   /* Connect to server with option CLIENT_MULTI_STATEMENTS */mysql_real_connect(..., CLIENT_MULTI_STATEMENTS);/* Now execute multiple queries */mysql_query(mysql,"DROP TABLE IF EXISTS test_table;\                   CREATE TABLE test_table(id INT);\                   INSERT INTO test_table VALUES(10);\                   UPDATE test_table SET id=20 WHERE id=10;\                   SELECT * FROM test_table;\                   DROP TABLE test_table");do{  /* Process all results */  ...  printf("total affected rows: %lld", mysql_affected_rows(mysql));  ...  if (!(result= mysql_store_result(mysql)))  {     printf(stderr, "Got fatal error processing query\n");     exit(1);  }  process_result_set(result); /* client function */  mysql_free_result(result);} while (!mysql_next_result(mysql));

具體看代碼:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <dlfcn.h>#include <mysql/mysql.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <errno.h>#include <termios.h>#include <mysql/mysql.h>void process_result_set(MYSQL       *mysql, MYSQL_RES *result){        int i =0;        unsigned int fieldnum;        //從結果集,擷取表頭資訊        MYSQL_FIELD *fields = mysql_fetch_fields(result);        fieldnum = mysql_field_count(mysql);        for (i=0; i<fieldnum; i++)        {            printf("%s\t", fields[i].name);        }        printf("\n");        //從結果集, 按照行擷取資訊資訊        MYSQL_ROW row = NULL;        //從結果集中一行一行的擷取資料        while (  row = mysql_fetch_row(result))        {            fieldnum = mysql_field_count(mysql);            //最佳化,我的行有多少列。。。。尋找這樣的api函數            for (i=0; i<fieldnum; i++) //經過測試 發現 不是以0結尾的指標數組。。            {                printf("%s\t", row[i]);            }            printf("\n");        }}int main(){    int         ret = 0, status = 0;    MYSQL       *mysql;    MYSQL_RES   *result;    MYSQL_ROW   row;    char        *query;    mysql = mysql_init(NULL);    mysql =mysql_real_connect(mysql, "localhost", "root", "123456", "mydb2", 0, NULL, CLIENT_MULTI_STATEMENTS);    if (mysql == NULL)    {        ret = mysql_errno(mysql);        printf("func mysql_real_connect() err\n");        return ret;    }    else    {        printf(" ok......\n");    }    /* execute multiple statements */status = mysql_query(mysql,"DROP TABLE IF EXISTS test_table;\CREATE TABLE test_table(id INT);\INSERT INTO test_table VALUES(10);\UPDATE test_table SET id=20 WHERE id=10;\SELECT * FROM test_table;\DROP TABLE test_table");    if (status)    {        printf("Could not execute statement(s)");        mysql_close(mysql);        exit(0);    }    /* process each statement result */    do {            /* did current statement return data? */            result = mysql_store_result(mysql);            if (result)            {                /* yes; process rows and free the result set */                process_result_set(mysql, result);                mysql_free_result(result);            }            else /* no result set or error */            {                if (mysql_field_count(mysql) == 0)                {                    printf("%lld rows affected\n",                    mysql_affected_rows(mysql));                }                else /* some error occurred */                {                    printf("Could not retrieve result set\n");                    break;                }            }        /* more results? -1 = no, >0 = error, 0 = yes (keep looping) */        if ((status = mysql_next_result(mysql)) > 0)                printf("Could not execute statement\n");    } while (status == 0);    mysql_close(mysql);}

以上就是MySQL入門之一次函數調用執行多條語句的內容。

接下來我們主要介紹了PHP實現mysqli批量執行多條語句的方法,結合執行個體形式分析了php串連mysqli並批量執行多條語句的相關操作技巧,

具體如下:

可以一次性的執行多個操作或取回多個結果集。

執行個體:


<?php$mysqli = new mysqli("localhost", "root", "111111", "test");/* check connection */if (mysqli_connect_errno()) {  printf("Connect failed: %s\n", mysqli_connect_error());  exit();}/* multi_query執行一個或多個針對資料庫的查詢。多個查詢用分號進行分隔。 */$query = "SELECT * from test where id = 1;";$query .= "SELECT name FROM test";/* 批量執行查詢 ,如果第一個查詢失敗則返回 FALSE。*/if ($mysqli->multi_query($query)) {  do {    /* 擷取第一個結果集 */    if ($result = $mysqli->store_result()) {      while ($row = $result->fetch_row()) {        printf("%s\n", $row[0]);      }      $result->free();    }    /* 檢查一個多查詢是否有更多的結果 */    if ($mysqli->more_results()) {      printf("-----------------\n");    }    //準備下一個結果集  } while ($mysqli->next_result());}/* close connection */$mysqli->close();?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.