Commands out of sync; you can't run this command now

前端 未结 21 2537
庸人自扰
庸人自扰 2020-11-21 11:35

I am trying to execute my PHP code, which calls two MySQL queries via mysqli, and get the error \"Commands out of sync; you can\'t run this command now\".

Here is th

相关标签:
21条回答
  • 2020-11-21 12:18

    I often hit this error and it's always when I run a stored procedure that I have been debugging in phpmyadmin or SQL Workbench (I am working in php connecting with mysqli).

    The error results from the fact that the debugging involves inserting SELECT statements at strategic points in the code to tell me the state of variables, etc. Running a stored procedure that produces multiple results sets in these client environments is fine, but it produces the "Commands out of sync" error when called from php. The solution is always to comment-out or remove the debugging selects so the procedure has only one result set.

    0 讨论(0)
  • 2020-11-21 12:20

    My problem was that I was using first prepare statement and then I was using mysqli query on the same page and was getting the error "Commands out of sync; you can't run this command now". The error was only then when I was using the code that had prepare statement.

    What I did was to close the question. and it worked.

    mysqli_stmt_close($stmt);

    My code

    get_category.php (here using prepare statement )

        <?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 (here 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>
    

    Something that just crossed my mind, I am also again adding connection variable via global $connection;. So I think basically a whole new set of query system is being started after ending of prepare statement with mysqli_stmt_close($stmt); and also I am adding these files and other stuff via include

    0 讨论(0)
  • 2020-11-21 12:21

    Once you used

    stmt->execute();
    

    You MAY close it to use another query.

    stmt->close();
    

    This problem was hunting me for hours. Hopefully, it will fix yours.

    0 讨论(0)
提交回复
热议问题