我使用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')。謝謝您的幫助! –