PHP SQL 全选并全部打印



我有一个SQL表,其中列ID(primary A_I(,uid,title,message,time,updatetime。 用户提交具有所有这些属性的帖子,除了更新时间,我需要在页面上为管理员显示所有帖子,如果没有,那么我只需要显示用户发布的帖子。

现在我在数据库中有两个帖子,但是当我尝试显示所有帖子时,它只显示第一篇文章

require 'header.php';
require 'navbar.php';
if($_SESSION['id'] == '')
{
    header('Location: confirm.php?state=5');
}
if($_SESSION['admin'] == '1')
{
    try
    {
        $poststmt = "SELECT ID, uid, title, message FROM posts";
        $postssql = $pdo->prepare($poststmt);
        $postssql->execute();
        $posts = $postssql->fetch();
        foreach($posts AS $ID=>$sectional)
        {
            echo $sectional;
        }

        }catch(PDOException $e){echo $e;}

}

那是因为$posts = $postssql->fetch();只获取 1 行。

您应该使用fetchAll()或执行以下操作:

while($row = $postssql->fetch()){
  print_r($row);
}

而不是忘记那个foreach循环。

最新更新