我正在尝试使用类编写select,但它不起作用


I am trying to write select using class, but it doesnot work

我想从DB newsId和title中调用2列,然后打印它。

我的代码如下:

class edit
{
    private $db;
    public function __construct()
    {
        $this->db = new Connection();
        $this->db = $this->db->dbConnect();
    }

    public function news($neswID, $title)
    {
        $sql = $this->db->prepare("SELECT newsID, title FROM `news`");
        $result = $sql->execute(array($newsID, $title));
        while ($sql->fetch($result)) {
            print $neswID . '<br>' . $title;
        }
    }
}

我这样称呼这个类:

include_once "test.php";
$object= new edit();
$object->news($newsID, $title);

我真的是个新手,谢谢你的帮助。

while循环应该是这样的,因为列名是newsIDtitle

$sql->execute(); 
while($row = $sql->fetch(PDO::FETCH_ASSOC)){
  echo $row['newsID'].'==='.$row['title'];
}

将代码更改为

$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC)){
  print $row['newsId'] .'<br>'. $row['title'] .'<br>';
}

prepare()返回一个PDOStatement