


mysqli executes multiple statements in batches and executes multiple statements in one function call
This article mainly shares with you the methods of mysqli batch execution of multiple statements and one function call to execute multiple statements. I hope you can have your own ideas through the examples in this article.
Supports the execution of multiple statements specified in a single string. To use this feature with a given connection, you must specify the CLIENT_MULTI_STATEMENTS option in the flags parameter to mysql_real_connect() when opening the connection. It can also be set for an existing connection by calling mysql_set_server_option(MYSQL_OPTION_MULTI_STATEMENTS_ON).
Commonly used routines:
/* 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));
Look at the code specifically:
#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); }
The above is the content of the MySQL entry to execute multiple statements in one function call.
Next, we mainly introduce the method of PHP to implement mysqli to execute multiple statements in batches, and analyze the related operating skills of PHP to connect to mysqli and execute multiple statements in batches in the form of examples.
The details are as follows :
You can perform multiple operations or retrieve multiple result sets at one time.
Example:
<?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(); ?>
Related recommendations:
MySQL implementation of executing multiple statements at one time and common problems
The above is the detailed content of mysqli executes multiple statements in batches and executes multiple statements in one function call. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When writing web applications using PHP, a MySQL database is often used to store data. PHP provides a way to interact with the MySQL database called MySQLi. However, sometimes when using MySQLi, you will encounter an error message, as shown below: PHPFatalerror:Calltoundefinedfunctionmysqli_connect() This error message means that PHP cannot find my

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam

Solution to php unable to connect to mysqli: 1. Open the "php.ini" file; 2. Find "mysqli.reconnect"; 3. Change "mysqli.reconnect = OFF" to "mysqli.reconnect = on".

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

If you encounter the following error message when using PHP to connect to a MySQL database: PHPWarning:mysqli_connect():(HY000/2002):Connectionrefused, then you can try to solve this problem by following the steps below. To confirm whether the MySQL service is running normally, you should first check whether the MySQL service is running normally. If the service is not running or fails to start, it may cause a connection refused error. you can

The running file of mysql is mysqld; mysqld is an executable file, which represents the Mysql server program. Executing this file can directly start a server process; and mysqld_safe is a startup script, which will indirectly call mysqld and also start a monitor. process.

When using the mysqli extension to connect and operate a MySQL database, you sometimes encounter the error PHPFatalerror:Calltoundefinedmethodmysqli::prepare(). This error is usually caused by the following reasons: PHP has insufficient support for the mysqli extension; the mysqli extension is not loaded or configured correctly; there are syntax errors in the PHP code; the MySQL server is not correctly configured or running

With the development of web applications, PHP language has been widely used in web development. In the PHP8.0 version, a new language feature was introduced - the multi-catch statement. What is a multi-catch statement? In previous PHP versions, developers needed to write multiple catch statements to handle multiple exception types. For example, the following code block shows the handling of two different exceptions: try{//Somecodethatmay
