Mysqli 不允许多个查询?

Mysqli doesn#39;t allow multiple queries?(Mysqli 不允许多个查询?)
本文介绍了Mysqli 不允许多个查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在 PHP 中运行一个脚本,该脚本使用循环为 MySQL 创建一个字符串查询.

I am running a script in PHP that uisng a loop creates a string query for MySQL.

执行脚本后出现以下错误:

After executing the script I get the following error:

您的 SQL 语法有错误;请查看手册对应于您的 MySQL 服务器版本以使用正确的语法'UPDATE BANNERS SET pos=1 WHERE BID=5; 附近更新横幅集pos=2 WHERE BID=1' 在第 2 行"

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE BANNERS SET pos=1 WHERE BID=5; UPDATE BANNERS SET pos=2 WHERE BID=1' at line 2"

在错误之后我回显查询,它看起来像这样:

right after the error I echo the query and it looks like this:

UPDATE BANNERS SET pos=0 WHERE BID=6;
UPDATE BANNERS SET pos=1 WHERE BID=5;
UPDATE BANNERS SET pos=2 WHERE BID=1;

当我将其复制并粘贴到 phpmyadmin 中时,它显然可以毫无问题地执行.

When I copy and paste it into phpmyadmin, it obviously gets executed without any problem.

有什么想法吗?

这是PHP代码:

有一个看起来像这样的数组:

There is an array that looks like this:

$order[0] = 'tr_6';
$order[1] = 'tr_5';
$order[2] = 'tr_1';

$query = "";

foreach($order as $pos => $value){
   $idvalue = str_replace('tr_','',$value);
   $query .= "UPDATE BANNERS  SET pos=$pos WHERE BID=$idvalue;
";
}

mysqli_query($connection,$query) or die(mysqli_error($connection)."<br/>$query");

谢谢!

推荐答案

mysqli 允许使用 mysqli_multiple_query 函数进行多次查询,如下所示:

mysqli allow multiple queries with mysqli_multiple_query function like this:

$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
", $row[0]);
            }
            mysqli_free_result($result);
        }
        /* print divider */
        if (mysqli_more_results($link)) {
            printf("-----------------
");
        }
    } while (mysqli_next_result($link));
}

请注意,每次查询后都需要使用分号.

note that you need to use semicolon after each query.

这篇关于Mysqli 不允许多个查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)