【问题标题】:Mysql not returning correct resultMysql没有返回正确的结果
【发布时间】:2013-04-07 13:08:51
【问题描述】:

我正在尝试检索所有处于活动状态且属于第 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 查询窗口中运行查询时,该查询有效。 那么为什么$cat_id = 1 时会收到第 12 类的帖子呢?

【问题讨论】:

  • 您没有使用其他问题中提供的解决方案。那为什么要问?然而,这个问题又是一个过于本地化的问题。不幸的是,问答网站不适合发现代码中的拼写错误。
  • 您是否发现retrieve_cat_posts() 函数中有任何问题。我想我已经把范围缩小到了。

标签: php mysql arrays select pdo


【解决方案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>';

有点奇怪,在 echo 语句中设置变量值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2014-09-13
    • 2012-08-01
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多