【问题标题】:php mysqli help, first line in DB not being returned?php mysqli 帮助,没有返回数据库中的第一行?
【发布时间】:2012-11-01 03:01:57
【问题描述】:

这是我的代码

     <?php require_once 'connect.php';

                    $sql = "SELECT * FROM `db-pages`";
                    $result = $mysqli->query($sql) or die($mysqli->error.__LINE__);
                        while ($row = $result->fetch_assoc()) {
   echo($row['pagetitle'].' - To edit this page <a href="editpage.php?id='.$row['id'].'">click here</a><br>');
 }  
                    }
      ?>

除了数据库中的 id=1 之外,我已向数据库中添加了几行,它会将它们全部返回。知道为什么吗?

【问题讨论】:

  • 结果是什么echo mysqli_num_rows($result)
  • 尝试检查返回的行数,然后使用echo $result-&gt;num_rows;
  • @GBD 你忘记了mysql 中的i。 @wiiliamsongibson:数据库中有多少数据?
  • 您可以在 [此处][1] [1] 找到并回答类似问题:stackoverflow.com/questions/2285600/…

标签: php mysql mysqli


【解决方案1】:

像这样尝试:

 while ($row = $result->fetch_assoc()) {
   echo($row['pagetitle'].' - To edit this page <a href="editpage.php?id='.$row['id'].'">click here</a><br>');
 }  

【讨论】:

  • 谢谢,我已经在上面添加了您的示例,并且在数据库中添加了超过 2 行。它正在工作,但第一行 id=1 从未显示。知道为什么吗?
  • 如果你打印了 mysqli_fetch_assoc(),会显示什么? print_r(mysqli_fetch_assoc());
【解决方案2】:

仔细检查标题并确保它没有任何会影响 php 输出它的内容。

还可以使用 htmlentities 转义所有数据库输出,这有助于在有人发挥创意的情况下进行良好实践。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    相关资源
    最近更新 更多