我使用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 />
<?
}
}
这只有回声的一个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 />
<?
}
}
同意。在重写$ stmt的内容之前,您只执行一次循环。 – Kevin
认为它可能是这样的。它在一个页面上工作,但我在某些页面上一直收到'无效的对象或资源mysqli_stmt'。我会编辑它会多一些代码和错误行。 –
还需要新的'$ db'(称为'$ db2')。谢谢您的帮助! –