sql 查询中为 foreach() 错误提供的参数无效
Invalid argument supplied for foreach() mistake in sql query
所以我在 php 中开发分页系统,我使用 sql 从数据库中获取所有帖子,这是我的代码:
$sql = 'SELECT * FROM wp_posts WHERE post_parent="0" ORDER BY ID DESC LIMIT $start, $per_page';
$articles = $db->query($sql);
$start 应设置为 0,$per_page 应设置为 20
但是当运行结果通过foreach循环时returns这个警告:
Warning: Invalid argument supplied for foreach() in C:\wamp\www\System-tips\index.php on line 29
Call Stack
在 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";
?>
我不确定这是否是你的问题,但你的字符串在这里:
'SELECT * FROM wp_posts WHERE post_parent="0" ORDER BY ID DESC LIMIT $start, $per_page'
... 包含在 '
单引号中。 PHP 为 PHP,仅当字符串包含在 "
双引号中时,您在此处进行的插值才有效。因此,作为第一步,请尝试将 "
换成 '
。
所以我在 php 中开发分页系统,我使用 sql 从数据库中获取所有帖子,这是我的代码:
$sql = 'SELECT * FROM wp_posts WHERE post_parent="0" ORDER BY ID DESC LIMIT $start, $per_page';
$articles = $db->query($sql);
$start 应设置为 0,$per_page 应设置为 20
但是当运行结果通过foreach循环时returns这个警告:
Warning: Invalid argument supplied for foreach() in C:\wamp\www\System-tips\index.php on line 29 Call Stack
在 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";
?>
我不确定这是否是你的问题,但你的字符串在这里:
'SELECT * FROM wp_posts WHERE post_parent="0" ORDER BY ID DESC LIMIT $start, $per_page'
... 包含在 '
单引号中。 PHP 为 PHP,仅当字符串包含在 "
双引号中时,您在此处进行的插值才有效。因此,作为第一步,请尝试将 "
换成 '
。