while($ stmt-> fetch())只循环一次

时间:2012-01-24 21:01:36

标签: php mysql prepared-statement

我正在使用while($stmt->fetch())来循环我从MySQL查询得到的结果,在某些条件下,我只得到一个结果。
我猜这是因为我有2 $stmt的。但我认为这种事情得到了支持。我想我犯了一个新手的错误,我已经习惯了很长时间没有准备好的陈述!

    $db = mysqlConnect();
    $stmt = $db->stmt_init();
    $stmt->prepare("SELECT id, uploadtype FROM uploads ORDER BY displayorder DESC");
    $stmt->execute();
    $stmt->bind_result($id, $uploadtype);
    while ($stmt->fetch()) {
        echo 'ID = ' . $id . '<br />';
        if ($uploadtype == 'single') {
            $stmt->prepare("SELECT title, description FROM singles WHERE id = ?");
            $stmt->bind_param('i', $id);
            $stmt->execute();
            $stmt->bind_result($title, $description);
            $stmt->fetch();
            ?>
            Title: <? echo $title; ?><br />
            Description: <? echo $description; ?><br />
            <a href="edit.php?id=<? echo $id; ?>">Edit</a><br />
            <?
        }
    }

这只是echo的一个ID。我猜这是问题,因为当我使用以下内容时,我会回复所有ID。

$db = mysqlConnect();
$stmt = $db->stmt_init();
$stmt->prepare("SELECT id, uploadtype FROM uploads ORDER BY displayorder DESC");
$stmt->execute();
$stmt->bind_result($id, $uploadtype);
while ($stmt->fetch()) {
    echo 'ID = ' . $id . '<br />';
}

如何解决这个问题?我想我不太了解准备好的陈述 编辑:
现在更改为此,但invalid object or resource mysqli_stmt在线<error>开始。

$db = mysqlConnect();
    $stmt = $db->stmt_init();
    if (!$limit) {
        $stmt->prepare("SELECT id FROM uploads WHERE uploadtype = 'youtube' ORDER BY displayorder DESC");
    } else {
        $stmt->prepare("SELECT id FROM uploads WHERE uploadtype = 'youtube' ORDER BY displayorder DESC LIMIT 0, ?");
        $stmt->bind_param('i', $limit);
    }
    $stmt->execute();
    $stmt->bind_result($id);
    while ($stmt->fetch()) {
        $stmt2 = $db->stmt_init();
        $stmt2->prepare("SELECT title, description, url FROM youtube WHERE id = ?");
        <error>$stmt2->bind_param('i', $id);
        <error>$stmt2->execute();
        <error>$stmt2->bind_result($title, $description, $url);
        <error>while ($stmt2->fetch()) {
            $title = stripslashes($title);
            $description = stripslashes($description);
            $url = stripslashes($url);
            ?>
            <class id="item">
                <?
                if ($gettitle) echo 'Title: ' . $title . '<br/>';
                if ($getdescription) echo 'Description: ' . $description . '<br />';
                ?>
                <iframe width="560" height="315" src="<? echo $url; ?>" frameborder="0" allowfullscreen></iframe>
            </class><br />
            <?
        }
    }

2 个答案:

答案 0 :(得分:3)

您需要另一个语句变量。将第二个用法(SELECT title...)替换为单独的变量(例如$stmt2,因为缺少更好的名称)。

    if ($uploadtype == 'single') {
        $stmt2 = $db->stmt_init();
        $stmt2->prepare("SELECT title, description FROM singles WHERE id = ?");
        $stmt2->bind_param('i', $id);
        ...

否则外循环中的下一个fetch将针对第二个语句运行。

答案 1 :(得分:1)

如果您正在执行此类嵌套查询,请不要使用相同的变量名称($stmt)。将内部语句命名为不同的名称。