从mysql数据库中提取特定数据

xwbd5t1u  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(450)

基本上,我有一张table叫 requests ,在该表中有一列名为 status . 默认情况下, status 设置为挂起,但也可以设置为已批准或已拒绝。
我试图显示状态设置为挂起的行中的数据,但不显示已批准或已拒绝的行。
我该怎么做?
我的pdo声明:

  1. try{
  2. $statement = $db->query("SELECT * FROM requests");
  3. $requests = $statement->fetchAll(PDO::FETCH_ASSOC);
  4. }catch (PDOException $ex){
  5. $result = flashMessage("An error occurred: " .$ex->getMessage());
  6. }

显示数据的页面:

  1. <tbody>
  2. <tr>
  3. <?php if(isset($request['status'] === 'pending')): ?>
  4. <th scope="row"><?= $request['id']; ?></th>
  5. <td><?= $request['username']; ?></td>
  6. <td><?= $request['artist']; ?></td>
  7. <td><?= $request['day']; ?></td>
  8. <td><?= $request['venue']; ?></td>
  9. <td><?= $request['city']; ?></td>
  10. <?php else: ?>
  11. <? # do nothing ?>
  12. <?php endif: ?>
  13. </tr>
  14. </tbody>

我当前的代码包含导致500错误的页面。
谢谢您!

0qx6xfy6

0qx6xfy61#

我认为500错误是由两个问题造成的,第一个问题:

  1. endif:

检查php文档中的替代控制结构-应该是

  1. endif;

那么,这个:

  1. <?php if(isset($request['status'] === 'pending')): ?>

不是如何限制你的结果只有那些“待定”状态。 isset 只接受变量,给它一个表达式 $request['status'] === 'pending' 是另一个分析错误。 isset 仅对未设置或设置为的变量返回false null .
但这并不重要,因为您应该在查询中按状态进行筛选。只需添加一个where子句,并在php代码中去掉该检查。

  1. "SELECT * FROM requests WHERE status = 'pending'"
krugob8w

krugob8w2#

您很可能得到错误500,因为您没有终止if语句。你需要一个分号。

  1. <tbody>
  2. <tr>
  3. <?php if(! empty($request['status']) && $request['status'] === 'pending')): ?>
  4. <th scope="row"><?= $request['id']; ?></th>
  5. <td><?= $request['username']; ?></td>
  6. <td><?= $request['artist']; ?></td>
  7. <td><?= $request['day']; ?></td>
  8. <td><?= $request['venue']; ?></td>
  9. <td><?= $request['city']; ?></td>
  10. <?php else: ?>
  11. <? # do nothing ?>
  12. <?php endif; ?>
  13. </tr>
  14. </tbody>

相关问题