所以我的工作在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 –