所以我的工作在PHP分頁系統上,我得到的地方,我從數據庫中獲得使用SQL的所有帖子的部分,這是我的代碼:無效的參數()錯誤
$sql = 'SELECT * FROM wp_posts WHERE post_parent="0" ORDER BY ID DESC LIMIT $start, $per_page';
$articles = $db->query($sql);
$開始應設置爲0,$ per_page至20
但通過foreach循環運行的結果,當它返回此警告:
警告:在C的foreach()提供的參數無效:\ WAMP \ WWW \系統提示\我ndex.php在線29 調用堆棧
當在sql查詢中手動輸入數字時,它完美地工作,我做錯了什麼?
編輯:這裏是完整的index.php代碼
<?php
include"header.php";
//Delete Auto Draft posts
$del = 'DELETE FROM wp_posts WHERE post_title="Auto Draft"';
$db->query($del);
//Receive all posts
$sql = 'SELECT * FROM wp_posts WHERE post_parent="0" ORDER BY ID DESC';
$counter = $db->query($sql);
//Amount of pages
$row_cnt = $counter->num_rows;
$per_page = 5;
$pages = ceil($row_cnt/$per_page);
//Receive current page
if(!isset($_GET['page'])) {
header("location: index.php?page=1");
}
else {
$page = $_GET['page'];
}
$start = (($page - 1)*$per_page);
$sql = 'SELECT * FROM wp_posts WHERE post_parent="0" ORDER BY ID DESC LIMIT $start, $per_page';
$articles = $db->query($sql);
foreach ($articles as $article) {
echo'
<article>
<h3>'. $article['post_title'] .'</h3>';
search_picture($article['post_content']);
preview($article['post_content']);
echo '<a href="#">Read the article...</a>';
echo'</article>';
}
function search_picture($content) {
$array = explode('<img',$content);
if (isset($array[1])) {
$picture = explode(' />',$array[1]);
echo '<img' . $picture[0] . ' />'; }
echo '
<style>
img {
width: 150px;
height: 100px;
float: left;
margin-right: 20px;
}</style>
';
}
function preview($content) {
$preview = explode('<!--more-->',$content);
echo '<p style="margin-right: 20px;">' . $preview[0] . '</p>';
}
include"footer.php";
?>
發佈'index.php' foreach。 –
你可以發佈'var_dump($ articles);'的結果(或部分如果它太大)嗎? –
var_dump返回:布爾值false –