我正在尝试检索所有处于活动状态且属于第 1 类的帖子。应该有 46 个帖子,但它输出 4 并且帖子来自第 12 类而不是第 1 类。
$cat_id = 1;
$limit = 12;
// Count posts
$count_posts = count_cat_posts($cat_id);
这是 count_posts 函数
// Count category posts
function count_cat_posts($cat_id){
$stmt = $dbh->prepare("SELECT * FROM mjbox_posts WHERE post_active = 1 AND cat_id = ?");
$stmt->bindParam(1,$cat_id);
$stmt->execute();
$rows = $stmt->fetchAll();
$count_posts = count($rows);
return $count_posts;
}
我将所有帖子存储在一个数组中
// Retrieve all active posts in this category and order by lastest first
$resultarray = retrieve_cat_posts($cat_id, $offset, $limit);
这是我正在使用的功能。
// Retrieve active posts
function retrieve_cat_posts($cat_id, $offset, $limit){
// Get all the posts
$stmt = $dbh->prepare(" SELECT p.post_id, post_year, post_desc, post_title, post_date, img_file_name, p.cat_id
FROM mjbox_posts p
JOIN mjbox_images i
ON i.post_id = p.post_id
AND i.cat_id = p.cat_id
AND i.img_is_thumb = 1
AND post_active = 1
AND p.cat_id = ?
ORDER BY post_date
DESC
LIMIT ?,?");
$stmt->bindParam(1, $cat_id, PDO::PARAM_INT);
$stmt->bindParam(2, $offset, PDO::PARAM_INT);
$stmt->bindParam(3, $limit, PDO::PARAM_INT);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$resultarray[] = $row;
}
return $resultarray;
}
我没有添加偏移变量,但它是正确的。
我输出这样的帖子:
foreach($resultarray AS $value){
$filename = substr($value['img_file_name'],9);
$cat_id = $value['cat_id'];
// Wraps image, title, category
echo '<div class="itemWrap">';
// Item Image
echo '<a href="post.php?post_id='.$value['post_id'].'" class="itemImageLink"><img class="itemImage" src="create_thumb.func.php?path=img/fs/'.$filename.'&save=0&width=160&height=120" alt="'. stripslashes(stripslashes($value['post_title'])) .'"></a>';
// Item Title
echo '<div class="itemTitle"><a href="post.php?post_id='.$value['post_id'].'" class="itemTitleLink">' .stripslashes(stripslashes($value['post_title'])). '</a></div>';
// Item Category
echo '<div class="itemCat"><a href="cat.php?cat_id='.$cat_id.'">'. $cat_name = get_cat_name($cat_id) .'</a></div>';
// close itemWrap
echo '</div>';
}
当我在 mysql 查询窗口中运行它时,该查询有效。那么,为什么它会从类别 12 中获取帖子$cat_id = 1
呢?