登录  /  注册

mysqli批量执行多条语句和一次函数调用执行多条语句方法

小云云
发布: 2018-02-03 10:37:24
原创
2045人浏览过

本文主要和大家分享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();
?>
登录后复制

相关推荐:

MySQL 一次执行多条语句的实现及常见问题

以上就是mysqli批量执行多条语句和一次函数调用执行多条语句方法的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号