2011-04-02 61 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