我正在尝试执行我的PHP代码,它通过mysqli调用两个MySQL查询,并得到错误<!>“;命令不同步;你现在不能运行这个命令<!> quot;。

以下是我正在使用的代码

<?php
$con = mysqli_connect("localhost", "user", "password", "db");
if (!$con) {
    echo "Can't connect to MySQL Server. Errorcode: %s\n". Mysqli_connect_error();
    exit;
}
$con->query("SET NAMES 'utf8'");
$brand ="o";
$countQuery = "SELECT ARTICLE_NO FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE % ? %";
if ($numRecords = $con->prepare($countQuery)) {
    $numRecords->bind_param("s", $brand);
    $numRecords->execute();
    $data = $con->query($countQuery) or die(print_r($con->error));
    $rowcount = $data->num_rows;
    $rows = getRowsByArticleSearch("test", "Auctions", " ");
    $last = ceil($rowcount/$page_rows);
}  else {

print_r($con->error);
}
foreach ($rows as $row) {
    $pk = $row['ARTICLE_NO'];
    echo '<tr>' . "\n";
    echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$row['USERNAME'].'</a></td>' . "\n";
    echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$row['shortDate'].'</a></td>' . "\n";
    echo '<td><a href="#" onclick="deleterec(\'Layer2\', \'' . $pk . '\')">DELETE RECORD</a></td>' . "\n";
    echo '</tr>' . "\n";
}
function getRowsByArticleSearch($searchString, $table, $max) {
    $con = mysqli_connect("localhost", "user", "password", "db");
    $recordsQuery = "SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME, date_format(str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s'), '%d %m %Y' ) AS shortDate FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE '%?%' ORDER BY str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s')" . $max;
    if ($getRecords = $con->prepare($recordsQuery)) {
        $getRecords->bind_param("s", $searchString);
        $getRecords->execute();
        $getRecords->bind_result($ARTICLE_NO, $USERNAME, $ACCESSSTARTS, $ARTICLE_NAME, $shortDate);
        while ($getRecords->fetch()) {
            $result = $con->query($recordsQuery);
            $rows = array();
            while($row = $result->fetch_assoc()) {
                $rows[] = $row;
            }
            return $rows;
        }
    }
}

我已经尝试过阅读,但我不确定该怎么做。我已经阅读了关于商店结果和免费结果的信息,但是这些在使用它们时没有任何区别。我不确定这个错误究竟出在哪一点,并且想知道它为什么会被引起,以及如何修复它。

通过我的调试语句,countQuery的第一个if循环甚至没有被输入,因为我的sql语法中的错误靠近'% ? %'附近。但是,如果我只选择*而不是尝试基于LIKE子句进行限制,我仍然会得到命令不同步错误。

有帮助吗?

解决方案

您不能同时拥有两个查询,因为默认情况下mysqli使用无缓冲的查询(对于预处理语句;对于vanilla mysql_query,它是相反的)。您可以将第一个获取到数组中并循环遍历,或者告诉mysqli缓冲查询(使用 $stmt->store_result() )。

有关详细信息,请参见此处

其他提示

我在我的C应用程序中解决了这个问题 - 这是我如何做到的:

  1. 从mysql论坛引用:

      

    在应用程序中使用分号分隔符终止查询时,会出现此错误。虽然从命令行或查询浏览器执行查询时需要使用分号分隔符终止查询,但请从应用程序内的查询中删除分隔符。

  2. 在运行我的查询并处理结果[C API:mysql_store_result()]之后,我迭代通过多个SQL语句执行发生的任何进一步的潜在待决结果,例如两个或多个select语句(背对背)没有处理结果)。

    事实是我的程序不会返回多个结果,但是在执行之前数据库不知道:[C API:mysql_next_result()]。我在一个循环(为了好的测量)中这样做,直到它返回非零。那是当前连接处理程序知道可以执行另一个查询(我缓存我的处理程序以最小化连接开销)。

    这是我使用的循环:

    for(; mysql_next_result(mysql_handler) == 0;) 
      /* do nothing */;
    
  3. 我不知道PHP,但我确信它有类似的东西。

我今天遇到了同样的问题,但仅限于使用存储过程时。这使查询的行为类似于多查询,因此您需要<!> quot; consume <!> quot;在进行另一次查询之前,其他结果可用。

while($this->mysql->more_results()){
    $this->mysql->next_result();
    $this->mysql->use_result();
}

每次使用$ mysqli - <!> gt;查询之前,我都会调用此函数 也适用于存储过程。

function clearStoredResults(){
    global $mysqli;

    do {
         if ($res = $mysqli->store_result()) {
           $res->free();
         }
        } while ($mysqli->more_results() && $mysqli->next_result());        

}

我使用CodeIgniter。 一台服务器好......这个可能比较旧...... 无论如何使用

$this->db->reconnect();

修正了它。

问题是MySQL客户端C库,其中构建了大多数MySQL API。问题是C库不支持同时执行查询,因此构建在其上的所有API也不支持。即使您使用无缓冲的查询。这就是编写异步MySQL API的一个原因。它使用TCP直接与MySQL服务器通信,并且有线协议 支持同时查询。

您的解决方案是修改算法,这样您就不需要同时进行两者,或者更改它们以使用缓冲查询,这可能是它们存在于C库中的原因之一(另一种是提供一种游标)。

要解决此问题,您必须在使用之前存储结果数据

$numRecords->execute();

$numRecords->store_result();

这就是全部

对于那些以前的答案没有帮助的人......这就是我的问题!!!

param绑定是<!> quot; dynamic <!> quot;所以我有一个变量来设置数据的参数,以便使用 bind_param 。所以这个变量是错误的,而不是抛出像<!>“错误的参数数据<!>”这样的错误。它说<!>“;不同步bla bla bla <!>”;所以我很困惑......

我希望它有所帮助!

使用后

stmt->execute();

您必须将其关闭才能使用其他查询。

stmt->close();

这个问题困扰了我好几个小时。希望它能解决你的问题。

我认为问题在于你在函数中建立了一个新的连接,然后在最后没有关闭它。为什么不尝试传入现有连接并重新使用它?

另一种可能性是你从while循环中取回。你永远不会完成那个外部获取。

检查您是否正确输入所有参数。如果定义并传递给函数的参数数量不同,则会抛出相同的错误。

这与原始问题无关,但我有相同的错误消息,这个帖子是谷歌的第一个热门,我花了一段时间来弄清楚问题是什么,所以它可能用于其它:

我不使用mysqli,仍然使用mysql_connect 我有一些简单的查询,但是一个查询导致所有其他查询在同一个连接中失败。

我使用mysql 5.7和php 5.6 我有一个数据类型<!>“JSON <!>”的表。显然,我的php版本无法识别mysql的返回值(php只是不知道如何处理JSON格式,因为内置的mysql-module太旧了(至少我认为))

现在我将JSON-Field-Type更改为Text(现在我不需要原生的mysql JSON功能)并且一切正常

如果要么使用Buffered或Unbuffered结果集来获取数据,首先必须清除内存中的提取数据,一旦获取了所有数据。因为在清除提取的内存之前,您无法在同一连接上执行另一个MYSQL过程。在脚本的右下方添加以下功能,以便解决问题

$numRecords->close(); or $numRecords->free(); // This clears the referencing memory, and will be ready for the next MYSQL fetch

PHP文档参考

我使用Doctrine DBAL QueryBuilder遇到了这个错误。

我使用QueryBuilder创建了一个查询,该查询使用了也使用QueryBuilder创建的列子选择。子选择仅通过$queryBuilder->getSQL()创建,而不是执行。创建第二个子选择时发生错误。通过在使用$queryBuilder->execute()之前使用$queryBuilder->connection临时执行每个子选择,一切正常。在执行当前准备的SQL之前,就好像连接<=>在创建新SQL时仍然处于无效状态,尽管每个子选择上都有新的QueryBuilder实例。

我的解决方案是编写没有QueryBuilder的子选择。

我的问题是我正在使用第一个prepare语句然后我在同一页面上使用mysqli查询并且收到错误<!>“;命令不同步;你现在不能运行这个命令<!> quot;。只有在我使用具有prepare语句的代码时才出现错误。

我所做的就是结束这个问题。它起作用了。

mysqli_stmt_close($语句);

我的代码

get_category.php(此处使用prepare语句)

    <?php


    global $connection;
    $cat_to_delete =    mysqli_real_escape_string($connection, $_GET['get'] );

    $sql = "SELECT category_name FROM categories WHERE category_id = ? ;";


    $stmt = mysqli_stmt_init($connection);

    if (!mysqli_stmt_prepare($stmt, $sql))
        $_SESSION['error'] = "Error at preaparing for deleting query. mysqli_stmt_error($stmt) ." & redirect('../error.php');

    mysqli_stmt_bind_param($stmt, 's', $cat_to_delete);

    if (!mysqli_stmt_execute($stmt))
        $_SESSION['error'] = "ERror at executing delete category ".mysqli_stmt_error($stmt) &
            redirect('../error.php');


    mysqli_stmt_bind_result($stmt, $cat_name);

    if (!mysqli_stmt_fetch($stmt)) {
        mysqli_stmt_error($stmt);
    }



    mysqli_stmt_free_result($stmt);
    mysqli_stmt_close($stmt);

admin_get_category_body.php(这里是mysqli)

        <?php

        if (isset($_GET['get']) && !empty($_GET['get']) )
        {
            include 'intodb/edit_category.php';
        }


        if (check_method('get') && isset($_GET['delete']) )
        {
            require 'intodb/delete_category.php';
        }


        if (check_method('get') && isset($_GET['get']) )
        {
            require 'intodb/get_category.php';
        }


        ?>

        <!--            start: cat body          -->

        <div     class="columns is-mobile is-centered is-vcentered">
            <div class="column is-half">
                <div   class="section has-background-white-ter box ">


                    <div class="has-text-centered column    " >
                        <h4 class="title is-4">Ctegories</h4>
                    </div>

                    <div class="column " >


                        <?php if (check_method('get') && isset($_GET['get'])) {?>
                        <form action="" method="post">
                            <?php } else {?>
                            <form action="intodb/add_category.php" method="post">
                                <?php } ?>

                                <label class="label" for="admin_add_category_bar">Add Category</label>
                                <div class="field is-grouped">

                                    <p class="control is-expanded">

                                        <?php if (check_method('get') && isset($_GET['get'])) {?>

                                            <input id="admin_add_category_bar" name="admin_add_category_bar_edit" class="input" type="text" placeholder="Add Your Category Here" value="<?php echo $cat_name; ?>">

                                            <?php


                                            ?>
                                        <?php } else {?>
                                            <input id="admin_add_category_bar" name="admin_add_category_bar" class="input" type="text" placeholder="Add Your Category Here">

                                        <?php } ?>
                                    </p>
                                    <p class="control">
                                        <input type="submit" name="admin_add_category_submit" class="button is-info my_fucking_hover_right_arrow" value="Add Category">
                                    </p>
                                </div>
                            </form>


                            <div class="">

                                <!--            start: body for all posts inside admin          -->


                                <table class="table is-bordered">
                                    <thead>
                                    <tr>
                                        <th><p>Category Name</p></th>
                                        <th><p>Edit</p></th>
                                        <th><p>Delete</p></th>
                                    </tr>
                                    </thead>

                                    <?php

                                    global $connection;
                                    $sql = "SELECT * FROM categories ;";

                                    $result = mysqli_query($connection, $sql);
                                    if (!$result) {
                                        echo mysqli_error($connection);
                                    }
                                    while($row = mysqli_fetch_assoc($result))
                                    {
                                        ?>
                                        <tbody>
                                        <tr>
                                            <td><p><?php echo $row['category_name']?></p></td>
                                            <td>
                                                <a href="admin_category.php?get=<?php echo $row['category_id']; ?>" class="button is-info my_fucking_hover_right_arrow" >Edit</a>

                                            </td>

                                            <td>
                                                <a href="admin_category.php?delete=<?php echo $row['category_id']; ?>" class="button is-danger my_fucking_hover_right_arrow" >Delete</a>

                                            </td>
                                        </tr>


                                        </tbody>
                                    <?php }?>
                                </table>

                                <!--           end: body for all posts inside admin             -->




                            </div>

                    </div>
                </div>


            </div>

        </div>
        </div>

我刚想到的东西,我也是通过全局$ connection;再次添加连接变量。所以我认为在使用mysqli_stmt_close($ stmt)结束prepare语句后,基本上会启动一整套新的查询系统;我还通过include

添加这些文件和其他内容
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top