PDO 显示结果 table

PDO display results in table

我正在尝试将准备好的语句的结果显示到 table。

这是查询和尝试 table:

<?php
  $s = "SELECT username,fullname,email,userlevel FROM users";
  $sth = $dbc->prepare($s, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
  $sth->execute();

  echo "<div><table class="demoTable">";
  echo "<thead><tr>" .
       "<th>Username</th>" .
       "<th>Fullname</th>" .
       "<th>Email</th>" .
       "<th>Userlevel</th>" .
       "</tr></thead>";
  echo "<tbody>";
  
  while($Row = $sth->fetchAll(PDO::FETCH_ASSOC)) {
    echo "<tr><td>".$Row['username']."</td>";
    echo "<td>".$Row['fullname']."</td>";
    echo "<td>".$Row['email']."</td>";
    echo "<td>".$Row['userlevel']."</td>";
    echo "</tr></tbody>";
  }
  echo "</table></div>";
?>

使用上面的方法,屏幕上没有显示任何内容。

当我使用以下内容时:

print_r($Row);

我可以在数组中看到如下所示的结果:

Array ( 
[0] => Array ( [username] => usr.sname [fullname] => Some Name [email] => some.name@company.com [userlevel] => 9 ) 
[1] => Array ( [username] => usr.aname [fullname] => Another Name [email] => another.name@company.com  [userlevel] => 1 ) 
[2] => Array ( [username] => usr.fname [fullname] => Final Name [email] => final.name@company.com [userlevel] => 1 ) // a few more

因此,查询工作正常。我只是无法在 table.

中显示数据

我怎样才能使这个工作?

我认为问题在于您正在使用 fetchAll,其中 returns 查询的所有行不是一行一行的。所以用这段代码替换你的 while 循环,我认为它应该可以工作。

   <?php
   $s = "SELECT username,fullname,email,userlevel FROM users";
   $sth = $dbc->prepare($s, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
   $sth->execute();

   echo "<div><table class='demoTable'>";
   echo "<thead><tr>" .
   "<th>Username</th>" .
   "<th>Fullname</th>" .
   "<th>Email</th>" .
   "<th>Userlevel</th>" .
   "</tr></thead>";
    echo "<tbody>";

    $allRows = $sth->fetchAll();
    foreach($allRows as $Row) {
        echo "<tr><td>".$Row['username']."</td>";
        echo "<td>".$Row['fullname']."</td>";
        echo "<td>".$Row['email']."</td>";
        echo "<td>".$Row['userlevel']."</td>";
        echo "</tr>";
    }
    echo "</tbody></table></div>";
    ?>

FWIW,以下对我来说很好...

<?php
  /*
  DROP TABLE users;
  
  CREATE TABLE users(user_id SERIAL PRIMARY KEY
  ,username VARCHAR(12) UNIQUE
  ,fullname VARCHAR(20) NOT NULL
  ,email VARCHAR(20) NOT NULL
  ,userlevel INT NOT NULL);
  
  INSERT INTO users VALUES 
  (1,'John','John Lennon','john@apple.corp',1),
  (2,'Paul','Paul McCartney','paul@apple.corp',1),
  (3,'George','George Harrison','george@apple.corp',2),
  (4,'Ringo','Ringo Starr','ringo@apple.corp',3);
  */
  
  require('path/to/connection/stateme.nts');
  $query = "SELECT username,fullname,email,userlevel FROM users";
  $data = $pdo->query($query)->fetchAll(PDO::FETCH_BOTH);
   // my connection is something like '$pdo = new PDO($dsn, $user, $pass, $options);'

  echo "
  <div><table class=\"demoTable\">
   <thead>
    <tr>
     <th>Username</th>
     <th>Fullname</th>
     <th>Email</th>
     <th>Userlevel</th>
    </tr>
   </thead>
   <tbody>
  ";

foreach($data as $row){
echo "
<tr>
 <td>{$row['username']}</td>
 <td>{$row['fullname']}</td>
 <td>{$row['email']}</td>
 <td>{$row['userlevel']}</td>
</tr>
";
}
  echo "</tbody></table></div>";
?>

输出:

Username Fullname        Email            Userlevel
John     John Lennon     john@apple.corp    1
Paul     Paul McCartney  paul@apple.corp    1
George   George Harrison george@apple.corp  2
Ringo    Ringo Starr     ringo@apple.corp   3