【问题标题】:Loop through PDO statment fetch(PDO::FETCH_OBJ) using a class使用类循环通过 PDO 语句 fetch(PDO::FETCH_OBJ)
【发布时间】:2016-01-19 21:41:08
【问题描述】:

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

<?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 变量相等的次数。 我尝试了一些不同的获取方法,例如: fetchAll() 和其他样式,但结果总是错误或仅重复一条记录。

请有人帮我写代码。

【问题讨论】:

  • 可能不是问题,但正如 PHP 手册中所述,rowCount() 就像您在 $stm-&gt;rowCount() 中所看到的那样,在 SELECT 语句中可能不准确。你会想要做一个COUNT()(示例 #2:php.net/manual/en/pdostatement.rowcount.php
  • 另外,您有一个getRelatedPosts() 方法,但您在脚本中使用了getSectionPosts()getRelatedPosts() 怎么了?
  • rowCount() 函数在这个例子中可以正常工作,感谢您的建议。
  • ^ 正如我所说,建议不要将其用于SELECT,我只是让您知道,以防您不知道。

标签: php oop pdo


【解决方案1】:

如果您想循环,请尝试在您的方法中循环:

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
    }

【讨论】:

  • 我刚刚添加了for 循环的样子,您都尝试了吗?
  • 这不会在索引文件中重复我的(div),
  • 另外,你得到了多少$num_of_rows?如果你说你有多行,它应该传递不止一个。
  • 是的,它们都不起作用,我想知道以这种方式使用 OOP 和 PDO 有什么问题
  • 我不怀疑,但是当你 echo $num_of_rows; 时,它会说多少行(因此循环)?
【解决方案2】:

在我的课堂上,我应该使用以下而不是当前的:

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

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

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

等等……

【讨论】:

    【解决方案3】:

    我正在尝试 PDO,但我遇到了与 PDO::FETCH_OBJ 相同的问题

    我在 xampp 5.6.30 中使用 PHP 5.6

    不用说 - 数据库名称是动物 - 它只有三列:animal_id、animal_type、animal_name

    以下测试代码可以正常工作并输出所有记录(在我的测试数据库中只有 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
    

    【讨论】:

      猜你喜欢
      • 2011-02-15
      • 2014-01-01
      • 2021-12-09
      • 1970-01-01
      • 2016-02-02
      • 2013-11-01
      • 2014-02-02
      • 1970-01-01
      相关资源
      最近更新 更多