2

嗨,所以我知道这似乎已经在这里得到了回答,但我尝试过的任何方法都没有奏效。基本上我要做的是调用一个插入一些数据的存储过程,并返回刚刚插入的 auto_increment 的 id。然后该语句关闭,我保留该 ID。

然后有趣的部分发生了。我进入一个循环,对于循环的每个实例,我需要调用另一个存储过程来插入一些数据,并返回最后一个 auto_increment 的 id。然后我会使用那个 id 去做更多的事情。但是,现在它在循环中失败了。它第一次执行没有问题,然后在准备下一次运行时它给了我错误Commands out of sync; you can't run this command now

我真的尝试过以某种方式从第一次使用 stmt->free() 时释放结果,但这没有用,或者释放 mysqli2 连接,但此时我所做的任何事情都没有奏效。任何提示或提示将不胜感激!

$insert_questionnaire_sql = "CALL insert_questionnaire_info(?, ?, ?, ?, ?, ?, ?)";

$questionnaire_insert_stmt = $mysqli->prepare($insert_questionnaire_sql);
$questionnaire_insert_stmt->bind_param("ssisiis", $meta[0], $meta[4], $length_of_questions, $user, $meta[2], $meta[3], $meta[1]);

//execute the statement
$success = $questionnaire_insert_stmt->execute();

$qn_id = -1;
//bind the id of the questionnaire that was just inserted
$questionnaire_insert_stmt->bind_result($qn_id);

//fetch the id
$questionnaire_insert_stmt->fetch();

//close the statement
$questionnaire_insert_stmt->close();


//next we insert each question into the database
$i = 0;
for($i; $i < count($Questions); $i++){
    //only if the question has been submitted
    if($Questions[$i]->submitted){
        //prepare the statement
        $insert_question_sql = "CALL insert_question_info(?, ?, ?, ?, ?, ?, ?)";
        $question_insert_stmt = $mysqli2->prepare($insert_question_sql) or die ($mysqli2->error);

        $type = -1;
        $width = -1;
        $height = -1;
        //count the number of answers
        $numAnswers = countNotDeletedAnswers($Questions[$i]);
        $text = $Questions[$i]->text;
        //figure out what kind of thing this is
        if($Questions[$i]->instruction == true){
            $type = 2;
        }
        else if($Questions[$i]->image == true){
            $type = 3;
            $width = $Questions[$i]->width;
            $height = $Questions[$i]->height;
            //if we have an image we want to put the path as the text
            $text = $Questions[$i]->path;
        }
        else{
            $type = 1;
        }

        //bind the params
        $question_insert_stmt->bind_param("isiisii", $qn_id, $text, $type, $numAnswers, $user, $width, $height);
        //execute
        $success = $question_insert_stmt->execute() or die ($mysqli2->error);
        //bind the id of the questionnaire that was just inserted
        $q_id = -1;
        $question_insert_stmt->bind_result($q_id);
        //fetch the id
        $data = $question_insert_stmt->fetch();

        //close the statement
        $question_insert_stmt->close();

    }

}
4

1 回答 1

1

CALL- 获取额外结果集的查询,如docs中所述。在这样的查询之后,尝试循环使用额外的结果集并释放它们,使用类似这样的东西:

function cleanUp(mysqli_stmt $stmt)
    {
        do
            {
                // $stmt->store_result();
                $stmt->free_result();
            }
        while($stmt->more_results() && $stmt->next_result());
    }

否则,如果您的过程返回任何结果集,您将收到此错误,这不是free()正确的。

于 2013-09-25T19:01:07.867 回答