使用类循环遍历PDO语句fetch(PDO::FETCH_OBJ)


Loop through PDO statment fetch(PDO::FETCH_OBJ) using a class

我在一个单独的文件中有一个名为posts的类:

<?php
class POSTS {
    //Start of class properties:
    private $db_connection;
    public $post_id;
    public $section_id;
    public $user_id;
    public $post_title;
    public $post_details;
    public $post_date;
    public $post_category;
    public $post_display;
    public $num_of_rows;
    public function getRelatedPosts($section_name, $category, $display) {
        $stm = $this->db_connection->prepare("SELECT * FROM posts WHERE section_name!=:Section_name AND category=:Category AND display=:Display ORDER BY id DESC");
        $stm->bindParam(":Section_name", $section_name);
        $stm->bindParam(":Category", $category);
        $stm->bindParam(":Display", $display);
        $stm->execute();
        $this->num_of_rows = $stm->rowCount();
        if ($this->num_of_rows >= 1) {
            $post_data = $stm->fetch(PDO::FETCH_OBJ);
            $this->post_id = $post_data->id;
            $this->section_id = $post_data->section_id;
            $this->user_id = $post_data->user_id;
            $this->post_title = $post_data->title;
            $this->post_details = $post_data->details;
            $this->post_date = $post_data->date;
            $this->post_category = $post_data->category;
            $this->post_display = $post_data->display;
        }
    }
}
?>

然后我想循环遍历索引文件中的结果:

                    $section_name = 'PHP';
                    $display = 'yes';
                    $POSTS->getRelatedPosts($section_name, $category $display);
                    $num_of_rows = $POSTS->num_of_rows;
                     if ($num_of_rows >= 1) {
                        for ($m=1; $m<=$num_of_rows; $m++) {
                            $post_id = $POSTS->post_id;
                            $section_id = $POSTS->section_id;
                            $user_id = $POSTS->user_id;
                            $post_title = $POSTS->post_title;
                            $post_details = $POSTS->post_details;
                            $post_date = $POSTS->post_date;
                        ?>
                        <div id="related_post">
                            <h4><a href=""><?php echo $post_title;?></a></h4>
                            <p><?php echo $post_details;?></p>
                        </div>
                        <?php
                        }
                    } else {
                        echo 'Sorry no related posts now!';
                    }

不幸的是,结果只有一条记录的重复次数与$num_of_rows变量相等。我尝试了一些不同的fetch方法,比如:fetchAll()和其他样式,但结果总是错误或只重复一条记录。

请有人帮我写代码。

如果你想循环,试着在你的方法中循环:

public function getRelatedPosts($section_name, $category, $display)
{
    $stm = $this->db_connection->prepare("SELECT * FROM posts WHERE section_name!=:Section_name AND category=:Category AND display=:Display ORDER BY id DESC");
    $stm->bindParam(":Section_name", $section_name);
    $stm->bindParam(":Category", $category);
    $stm->bindParam(":Display", $display);
    $stm->execute();
    $this->num_of_rows = $stm->rowCount();
    if ($this->num_of_rows >= 1) {
        while($post_data = $stm->fetch(PDO::FETCH_OBJ)) {
            $this->post_id[] = $post_data->id;
            $this->section_id[] = $post_data->section_id;
            $this->user_id[] = $post_data->user_id;
            $this->post_title[] = $post_data->title;
            $this->post_details[] = $post_data->details;
            $this->post_date[] = $post_data->date;
            $this->post_category[] = $post_data->category;
            $this->post_display[] = $post_data->display;
       }
    }
}

你的循环可能是这样的:

for ($m=1; $m<=$num_of_rows; $m++) {
    $post_id = $POSTS->post_id[$m];
    $section_id = $POSTS->section_id[$m];
    $user_id = $POSTS->user_id[$m];
    $post_title = $POSTS->post_title[$m];
    $post_details = $POSTS->post_details[$m];
    $post_date = $POSTS->post_date[$m];
?>
    <div id="related_post">
        <h4><a href=""><?php echo $post_title;?></a></h4>
        <p><?php echo $post_details;?></p>
    </div>
<?php
    }

在我的类中,我应该使用以下代码代替当前的:

$results = $stm->fetchAll(PDO::FETCH_OBJ);

我将不会得到我当前的类属性的任何优势。在索引文件中,我将使用foreach循环遍历$results,如下所示:

foreach($results as $post){
  $post_title = $post->title;
  $post_details = $post->details;
}

我正在试验PDO,我有同样的问题与PDO::FETCH_OBJ

我使用PHP 5.6在xampp 5.6.30

不用说了- DB的名字是动物-只有三列:animal_id, animal_type, animal_name

下面的测试代码运行良好并输出所有记录(在我的测试DB中只有11条)

$stmt = $dbh->prepare("SELECT * FROM animals");
$stmt->execute();
if($stmt->rowCount() > 0) {
    while($obj = $stmt->fetch(PDO::FETCH_OBJ)) {
        echo $obj->animal_type . " " . $obj->animal_name . "<br>";
    }
}

也许您可以在循环中插入计数器,或者更好的是,使查询适当地限制范围。

无论如何,上面的代码,如预期的那样输出 后面的内容
kookaburra bruce
emu bruce
goanna bruce
dingo bruce
kangaroo bruce
wallaby bruce
wombat bruce
koala bruce
kiwi cambiato
pippo zollo
pippz zoll2