2015-06-26 214 views
0

所以我的工作在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"; 
?> 
+0

发布'index.php' foreach。 –

+0

你可以发布'var_dump($ articles);'的结果(或部分如果它太大)吗? –

+0

var_dump返回:布尔值false –

回答

0

我不知道如果这是你的问题,但你的字符串在这里:

'SELECT * FROM wp_posts WHERE post_parent="0" ORDER BY ID DESC LIMIT $start, $per_page' 

...被封闭在'单引号。 PHP是PHP,插入如你在这里只会在字符串被包含在"双引号中。因此,作为第一步,请尝试使用'来切换您的"

+0

omg就是这么简单-.-,非常感谢! –

+0

完全没有问题:)基本上与'''中的字符串的评估方式相比'''有更多的内插和连接之间的性能差异,有时一个比另一个更合适:) – d0ug7a5