Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 而($stmt->;fetch())只循环一次_Php_Mysql_Prepared Statement - Fatal编程技术网

Php 而($stmt->;fetch())只循环一次

Php 而($stmt->;fetch())只循环一次,php,mysql,prepared-statement,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 disp

我使用
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 />
            <?
        }
    }

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


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

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


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

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

如果要执行这样的嵌套查询,请不要使用相同的变量名(
$stmt
)。将内部声明命名为不同的名称。

同意。您在重写$stmt的内容之前只执行了一次循环。我想可能是这样的。它只在一个页面上工作,但我在某些页面上不断得到
无效的对象或资源mysqli\u stmt
。我将编辑更多的代码和错误行。还需要新的
$db
(称为
$db2
)。谢谢你的帮助!同意。您在重写$stmt的内容之前只执行了一次循环。我想可能是这样的。它只在一个页面上工作,但我在某些页面上不断得到
无效的对象或资源mysqli\u stmt
。我将编辑更多的代码和错误行。还需要新的
$db
(称为
$db2
)。谢谢你的帮助!
$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 />';
}
$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 />
            <?
        }
    }
    if ($uploadtype == 'single') {
        $stmt2 = $db->stmt_init();
        $stmt2->prepare("SELECT title, description FROM singles WHERE id = ?");
        $stmt2->bind_param('i', $id);
        ...