2013-09-25 44 views
2

嗨,我知道這是在此之前似乎已經回答的問題,但沒有任何我已經嘗試過的工作。基本上我想要做的是調用插入一些數據的存儲過程,並返回剛插入的auto_increment的id。然後,該聲明關閉,我保持身份證。無法在for循環中執行查詢

然後有趣的部分發生。我進入一個循環,對於循環的每個實例,我需要調用另一個插入一些數據的存儲過程,並返回最後一個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(); 

    } 

} 
+0

不要在循環內部準備語句。這完全破壞了擁有它們的目的。準備OUTSIDE循環,然後在循環內重複執行它們。 –

+0

@MarcB我把它放在循環之外,沒有我沒有收到錯誤,但它仍然不會執行第二次。只有一件事是插入我的數據庫 – Pompey

回答

1

CALL -queries獲取附加結果集,如在docs提及。這樣的查詢後嘗試循環throght額外的結果集,並釋放他們,使用這樣的事情:

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

否則,你會得到這個錯誤,如果你的程序返回任何結果集,這不是free()ð正確。

+0

所以我會從我這裏for循環調用這個?:cleanUp($ question_insert_stmt),但這只是告訴我,$ get_result()是一個未定義的方法。 – Pompey

+0

@AndrewMoldovan好吧,看起來像'mysqlnd'沒有安裝在你的服務器上。我會很快糾正我的答案。 – BlitZ

+0

@AndrewMoldovan試試這個。 – BlitZ