【问题标题】:How do I echo out everything in a SQL table?如何回显 SQL 表中的所有内容?
【发布时间】:2018-05-16 10:27:12
【问题描述】:

我正在尝试从 sql 中回显表中的每一件事,我的代码如下

$stmt = $link->prepare("SELECT * FROM articles");
$stmt->execute();
$stmt->store_result();
if (mysqli_stmt_num_rows($stmt) >= 1) {
  $result = mysqli_stmt_get_result($stmt); //get result object
  while ($row = mysqli_fetch_assoc($result)){ //get associative array
      $news = $row['title'];
  }
}

不行,返回mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given

我已经完成了我的研究,但实际上没有任何效果:(

【问题讨论】:

    标签: php mysql sql


    【解决方案1】:

    您不需要为此 SELECT 查询使用准备好的语句,因为您没有指定任何内容 WHERE

    $query = 'SELECT * FROM articles';
    
    if ($result = $link->query($query)) {
        while ($row = $result->fetch_assoc()) {
            $news = $row['title'];
        }
    }
    

    要获得非常好的深入答案,请查看:https://stackoverflow.com/a/11575617/1427345

    【讨论】:

    • 非常感谢!在旁注中,我如何回显每个结果?我已经在查询中添加了LIMIT 0,25 以限制搜索。到目前为止,我正在尝试使用foreach ($row as $row['content']) 作为递归方法,但到目前为止我还没有运气,它只会回显数据库中的最新数据。当它的foreach ($result as $row['content']),我得到Notice: Array to string conversion,它不能回显“内容”数组----没关系,我知道了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-04-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    相关资源
    最近更新 更多