mysqli::prepare() [mysqli.prepare]:在新语句准备发生之前必须获取所有数据

问题描述 投票:0回答:1

我想从表中获取条目(由不同的人)。现在我想在另一个表中查找每个提取的人的名字,看看他是否有照片。 我的代码如下,但它不起作用,我收到以下错误:

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();
}
php mysqli
1个回答
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();
}
© www.soinside.com 2019 - 2024. All rights reserved.