2011-08-10 39 views
0

我在PHP中使用MYSQL語句綁定了一些參數。當在下面標記的行上計數($ posts)> 1時,它會拋出一個錯誤。誰知道我做錯了什麼? 錯誤是:調用一個非對象的成員函數bind_param()。它還報告comman不同步?(下面標線)mysqli :: prepare沒有返回一個對象?

<?php 

include '../../main/mainFunctions2.php'; 

$futurePosts = json_decode($_POST['futurePosts']); 

$repeatSerie = null; 
if(count($posts) > 1){ 

    //Get new repeatSeries 
    $stmt = $mysqli->prepare(" 
    SELECT repeatSerie 
    FROM timeSpaces_futurePosts 
    ORDER BY repeatSerie DESC 
    LIMIT 1 
    "); 
    $stmt->execute(); 
    $stmt->bind_result($repeatSerie); 
    $stmt->fetch(); 
    $repeatSerie = ((int)$repeatSerie + 1); 
} 
$timeStamp = time(); 
foreach($posts as $fp){ 
    $title = $fp->title; 
    $startDate = $fp->startDate; 
    $endDate = $fp->endDate; 
    $startTime = $fp->startTime; 
    $endTime = $fp->endTime; 
    $location = $fp->location; 
    $latLong = $fp->latLong; 
    $info = $fp->info; 
    $photoId = $fp->photoId; 
    $invited = $fp->invited; 
    if($invited != null){ 
     $invited = 1; 
    }else{ 
     $invited = 0; 
    } 
    $reminderType = $fp->reminderType; 
    $reminderTimeStamp = $fp->reminderTimeStamp; 
    $repeatSerie = $repeatSerie; 

    $stmt = $mysqli->prepare(" 
    INSERT INTO futurePosts (profileId, title, startDate, endDate, startTime, endTime, location, latLong, info, photoId, invited, reminderType, reminderTimeStamp, repeatSerie) 
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" 
    ); 
    $stmt->bind_param('isssiisssiisii', $profileId, $title, $startDate, $endDate, $startTime, $endTime, $location, $latLong, $info, $photoId, $invited, $reminderType, $reminderTimeStamp, $repeatSerie); 
    //The line above: Call to a member function bind_param() on a non-object 
    $stmt->execute(); 
    $futurePostId = $mysqli->insert_id; 

    if($invited == 1){ 
     foreach($fp->invited as $friendsId){ 
      $friendsId = $friendsId; 
      $stmt = $mysqli->prepare(' 
      INSERT INTO futurePosts_invited (profileId, futurePostId, timeStamp) 
      VALUES (?, ?, ?) 
      '); 
      $stmt->bind_param('iii', $friendsId, $futurePostId, $timeStamp); 
      $stmt->execute(); 
     } 
    } 
} 

echo 'TRUE'; 


?> 
+0

可能相關(我看不出SQL有什麼問題,但肯定會出現問題):[如何從PDO中擠出錯誤信息?](http://stackoverflow.com/ q/3726505) –

+0

命令不同步:現在不能使用prepare語句。那種錯誤是什麼?我用mysqli_report(MYSQLI_REPORT_ALL)得到了這個;在同一行上 – einstein

+0

我還在錯誤日誌 – einstein

回答

3

這很可能是因爲$stmt = $mysqli->prepare(...);線由於SQL語法錯誤失敗。試着回顯$mysqli->error看看它有什麼問題。

在執行您的SELECT聲明並且向MySQL發出任何其他查詢之前,請嘗試調用$stmt->store_result();

備註:您應該在foreach循環之前準備好您的對帳單。這會讓你獲得一些性能提升,因爲語句只會被編譯一次,並且每次循環運行時只有參數會被髮送到服務器。

+0

我認爲這實際上會解決這個問題 - 您正試圖在foreach()循環內再次準備,而不會在mysqli語句中調用「close」,導致「命令不同步」錯誤。 – TML

+0

我不確定那個@TML(也就是說,我非常肯定它不是這種情況)。爲'$ stmt'變量賦值第二個語句應該解引用前一個語句對象,並將其作爲對象銷燬的一部分自動關閉。 – Mchl

+0

你可能是對的 - 我實際上沒有太多的mysql用戶 - 但我很確定在這裏父母$ mysqli對象仍然知道它有一個開放的準備語句,你必須關閉它/釋放結果停止獲取「命令不同步」錯誤。 – TML

0

如果發生錯誤 ,mysqli_prepare()返回一個語句對象或FALSE。

相關問題