2013-04-07 130 views
-2

我试图找回那些活跃在类别1应该有46个员额的所有帖子,但它输出4和职位是从12类没有类别1.mysql的不返回正确的结果

$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查询窗口中运行它的查询工作。 那么,为什么从$cat_id = 1获得类别12的帖子呢?

+0

您没有使用您在其他问题中给出的解决方案。为什么要问?尽管如此,这个问题又是一个过于局限的问题。不幸的是,问答网站不适合在您的代码中发现拼写错误。 – 2013-04-07 13:41:47

+0

您是否在'retrieve_cat_posts()'函数中看到任何错误。我认为我把它缩小到了那里。 – crm 2013-04-07 13:52:27

回答

-1

在SELECT查询中使用JOIN而不是LEFT JOIN需要至少有一个图像存在int mjbox_images,它符合规定的条件。

而且这条线

echo '<div class="itemCat"><a href="cat.php?cat_id='.$cat_id.'">'. $cat_name = get_cat_name($cat_id) .'</a></div>'; 

有些离奇,设定回波语句内变量的值。

+0

查询本身没有任何问题,但谢谢 – crm 2013-04-07 13:53:46