2011-04-02 74 views
1

我有一個mysqli數據獲取問題。我將嘗試通過示例來解釋我的問題:mysqli獲取問題

我想從表中獲取條目(由不同的人員)。現在我想在另一個表格中查找每個獲取的人的姓名,並查看他是否有任何照片。 我的代碼如下,但其沒有工作,我收到以下錯誤:

mysqli::prepare() [mysqli.prepare]: All data must be fetched before a new statement prepare takes place 
    Call to a member function fetch() on a non-object in ... 

我的代碼:

if ($stmt = $this->mysqli->prepare("SELECT entry, author, time FROM messages WHERE user = ?")) { 
      $stmt->bind_param("s", $user_name); 
      $stmt->execute(); 
      $stmt->bind_result($entry, $author, $time); 

     while ($stmt->fetch()) {    
       if ($stmt = $this->mysqli->prepare("SELECT photo_id FROM photos WHERE user = ?")) { 
        $stmt->bind_param("s", $author); 
        $stmt->execute(); 
        $stmt->bind_result($photo_id); 
       } 
      //echo $photo_id; 
     }  
    $stmt->close(); 
    } 

我會任何幫助非常感謝。

+0

檢查我的答案。它適合你嗎?附:如果有,請不要忘記接受答案。 – Rihards 2011-04-02 22:19:00

回答

3

將第二條語句分配給新變量,以便它不會覆蓋第一個變量並導致「所有數據必須被提取..」錯誤。

if ($stmt = $this->mysqli->prepare("SELECT entry, author, time FROM messages WHERE user = ?")) { 
     $stmt->bind_param("s", $user_name); 
     $stmt->execute(); 
     $stmt->bind_result($entry, $author, $time); 

     while ($stmt->fetch()) {    
      if ($st = $this->mysqli->prepare("SELECT photo_id FROM photos WHERE user = ?")) { 
       $st->bind_param("s", $author); 
       $st->execute(); 
       $st->bind_result($photo_id); 
      } 
      //echo $photo_id; 
      $st->close(); 
     }  
    $stmt->close(); 
} 
+0

非常感謝您的幫助。分配第二個變量解決了一半的問題,其餘的我需要使用'$ stmt-> store_result();'。 – sudeep 2011-04-02 22:53:00

+0

@sudeep,很高興爲您效勞。 :) 祝你好運! – Rihards 2011-04-02 23:06:30