2015-06-26 215 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