Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Mysql没有返回正确的结果_Php_Mysql_Arrays_Select_Pdo - Fatal编程技术网

Php Mysql没有返回正确的结果

Php Mysql没有返回正确的结果,php,mysql,arrays,select,pdo,Php,Mysql,Arrays,Select,Pdo,我正在尝试检索所有活跃的和在类别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->prepar

我正在尝试检索所有活跃的和在类别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>';
                }
foreach($resultarray作为$value){
$filename=substr($value['img\u file\u name'],9);
$cat_id=$value['cat_id'];
//包装图像、标题、类别
回声';
//项目图像
回声';
//项目名称
回声';
//项目类别
回声';
//关闭项目包装
回声';
}
当我在mysql查询窗口中运行该查询时,它就会工作。
那么,当
$cat_id=1
时,为什么要从类别12获取帖子呢?

在SELECT查询中使用JOIN而不是LEFT JOIN,需要至少有一个符合所述条件的图像存在于mjbox_图像中

还有这条线

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

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

您没有使用其他问题中给出的解决方案。那为什么要问呢?然而,这个问题又是一个过于本地化的问题。不幸的是,问答网站不适合识别代码中的拼写错误。您是否发现
检索\u cat\u posts()
功能中有任何错误。我想我把范围缩小到那里了。