【发布时间】:2015-03-08 17:35:12
【问题描述】:
我正在使用此代码从 mysql 中检索与 usertype="user" 行关联的所有记录
class user{
public function getnews(){
$sql5 = "SELECT * FROM news WHERE usertype = 'user'";
$result = mysqli_query($this->db,$sql5);
$data = '';
while($row = mysqli_fetch_assoc($result)){
$data = '<article>';
$data .= '<h3>' . $row['title'] . '</h3>';
$data .= '<p>' . $row['content'] . '</p>';
$data .= '<footer>By: ' . $row['whoposted'] . ' / ' . $row['dateposted'] . '</footer>';
$data .= '</article>';
}
echo $data;
}
}
然后显示记录。
$user = new user();
$user->getnews()
我的数据库的结构是。
id, title, content, whoposted, dateposted, usertype.
并假设我有一条记录具有 usertype="admin"
id=autoincrement, title="this is the title", content="this is the content", dateposted="2015/03/08", usertype="admin"
还有 5 条用户类型为“user”的记录
id=autoincrement, title="this is the title", content="this is the content", dateposted="2015/03/08", usertype="user"
现在,正如您从我的 mysql 查询中看到的那样,我想获取所有具有 usertype="user" 的记录,并且它假设显示所有记录,除了一个具有 usertype="admin" 的记录但它只显示一条记录。
有人知道我的代码有什么问题吗?
任何帮助将不胜感激。谢谢!
【问题讨论】:
-
连接运算符 (.=) 在 while 循环中缺少第一行
$data = '<article>';应该是$data .= '<article>';