【发布时间】:2016-07-24 02:10:50
【问题描述】:
晚上好,我是 OOP 的新手,我正在尝试使用 MYSQLI fetch assoc 获得一些结果,但我有一个问题:在无限循环中只返回一个结果。请查看下面的代码,让我知道问题可能是什么:
class Database{
private $host;
private $user;
private $password;
private $db;
private $mysqli;
function __construct($host,$user,$pass,$data) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->data = $data;
$this->mysqli = new mysqli($this->host, $this->user, $this->pass, $this->data);
}
public function GetNewsArticles(){
$querystr="SELECT newsarticle.Id, newsarticle.Title from newsarticle GROUP BY newsarticle.Id ORDER BY newsarticle.id DESC";
return $this->mysqli->query($querystr);
}
}
$db= new Database("localhost","root","","news");
$db->GetNewsArticles();
while($row = $db->GetNewsArticles()->fetch_assoc()){
echo $row["Id"];
}
【问题讨论】: