我想从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);
我真的很新,谢谢你的帮助。
答案 0 :(得分:0)
将您的代码更改为
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC)){
print $row['newsId'] .'<br>'. $row['title'] .'<br>';
}
prepare()返回PDOStatement
答案 1 :(得分:0)
您的while循环应该是这样的,因为您的列名是newsID
和title
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC)){
echo $row['newsID'].'==='.$row['title'];
}