gpt4 book ai didi

php - 我正在尝试使用类编写选择,但它不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 22:05:07 24 4
gpt4 key购买 nike

我想从 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'];
}

关于php - 我正在尝试使用类编写选择,但它不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20121827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com