mysqli批次執行多條語句和一次函數呼叫執行多條語句方法
本文主要跟大家分享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(); ?>
相關推薦:
以上是mysqli批次執行多條語句和一次函數呼叫執行多條語句方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在使用PHP編寫Web應用程式時,經常會使用MySQL資料庫來儲存資料。 PHP提供了一種與MySQL資料庫互動的方法,稱為MySQLi。然而,有時在使用MySQLi時,會遇到一個錯誤訊息,如下所示:PHPFatalerror:Calltoundefinedfunctionmysqli_connect()這個錯誤訊息意味著PHP無法找到my

PDOPDO是一個物件導向的資料庫存取抽象層,它為PHP提供了一個統一的接口,允許您使用相同的程式碼與不同的資料庫(如Mysql、postgresql、oracle)進行互動。 PDO隱藏了底層資料庫連線的複雜性,簡化了資料庫操作。優缺點優點:統一接口,支援多種資料庫簡化資料庫操作,降低開發難度提供預處理語句,提高安全性支援事務處理缺點:效能可能比原生擴充稍低依賴外部函式庫,可能會增加開銷演示程式碼使用PDO連線mysql資料庫:$db=newPDO("mysql:host=localhost;dbnam

php無法連接mysqli的解決方法:1.開啟「php.ini」檔案;2、找到「mysqli.reconnect」;3、將「mysqli.reconnect = OFF」改成「mysqli.reconnect = on」即可。

如果你使用PHP連接MySQL資料庫時遇到了以下錯誤提示:PHPWarning:mysqli_connect():(HY000/2002):Connectionrefused那麼你可以嘗試按照下面的步驟來解決這個問題。確認MySQL服務是否正常運作首先應該檢查MySQL服務是否正常執行,如果服務未運行或啟動失敗,就可能會導致連線被拒絕的錯誤。你可

如何在PHP中使用MySQLi建立資料庫連線:包含MySQLi擴充(require_once)建立連線函數(functionconnect_to_db)呼叫連線函數($conn=connect_to_db())執行查詢($result=$conn->query())關閉連線( $conn->close())

mysql的運行文件是mysqld;mysqld是一個可執行文件,代表著Mysql伺服器程序,執行這個文件可以直接啟動一個伺服器進程;而mysqld_safe是一個啟動腳本,它會間接調用mysqld,並且還會順帶啟動一個監控進程。

當使用mysqli擴充來連接和操作MySQL資料庫時,有時會遇到PHPFatalerror:Calltoundefinedmethodmysqli::prepare()的錯誤。這個錯誤通常是由以下幾個原因引起的:PHP對mysqli擴充功能的支援不足;mysqli擴充沒有正確載入或配置;PHP程式碼有語法錯誤;MySQL伺服器沒有正確配置或正在執行

隨著Web應用程式的發展,PHP語言在Web開發中得到了廣泛應用。而在PHP8.0版本中,一個新的語言特性被引入-multi-catch語句。什麼是multi-catch語句?在先前的PHP版本中,對於多個異常類型的處理,開發人員需要寫多個catch語句。例如,如下程式碼區塊展示了兩種不同異常的處理:try{//Somecodethatmay
