通过用户ID从数据库获取帖子-PHP PDO

时间:2019-10-08 20:32:07

标签: php sql pdo

我的数据库:

My database

这是我用于检索帖子的php代码:示例post.php?id=12

<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    include_once('../connection.php');
    include_once('../comments.php');
    include_once('../gallery.php');
    class Objects{
        //fetch all posts
        public function get_information(){
            global $pdo;

            $query = $pdo->prepare('SELECT * FROM `post` LEFT JOIN post_gallery ON  post.post_id = post_gallery.user WHERE post_cat = "Cars"');
            $query->execute();

            return $query->fetchAll(PDO::FETCH_ASSOC);
        }
            //fetch post data by post id 
            public function fetch_data($pid){
            global $pdo;

            $query = $pdo->prepare('SELECT * FROM `post` LEFT JOIN post_gallery ON  post.post_id = post_gallery.user WHERE post_cat = "Cars"');
            $query->BindValue(1,$pid);
            $query->execute();

            return $query->fetch();
        }
    }
?>

此代码用于检索postspost_gallery信息和图像。问题是,当用户单击随机帖子并转到该页面上的post.php?id=12时,将显示由post_cat = "Cars"而非?id=12定义的所有帖子。有人知道我错在哪里吗?

0 个答案:

没有答案