【问题标题】:Display Array Values from SQL Table Rows显示 SQL 表行中的数组值
【发布时间】:2019-03-09 05:23:08
【问题描述】:

categoryID
   10
   20
   30

例如。以上是我的categoryID 列,其值为 10、20 和 30 PHP MySQL。我想要做的是使用数组来回显这些值。喜欢->10 20 30。下面是我的代码。在我的代码中。我刚刚将来自categoryID 的所有行数据存储到我的$array 变量中。我担心的是。如何回显所有值?

感谢您的帮助!

<?php
include ("dbconnect.php");

$sql = "SELECT categoryID FROM post";
$result = mysqli_query($con, $sql);

$array = array();

while($row = mysqli_fetch_assoc($result)) {
   $array[] = $row;
}

?>

【问题讨论】:

    标签: php sql arrays


    【解决方案1】:

    使用 $array 变量如下:-

    $array[] = $row['categoryID'];
    

    并打印数组尝试:-

    print_r($array);
    

    并将它们打印为 10 20 30 尝试以下操作:-

    echo implode(' ',$array);
    

    【讨论】:

      【解决方案2】:

      最好的方法是使用json_encode()函数

      回显 json_encode($array);

      所以试试这个

      <?php
      include ("dbconnect.php");
      
      $sql = "SELECT categoryID FROM post";
      $result = mysqli_query($con, $sql);
      
      $array = array();
      
      while($row = mysqli_fetch_assoc($result)) {
         $array[] = $row;
      }
      
      
          echo json_encode($array);
          exit;
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 2022-01-13
        • 2016-11-25
        • 2015-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-31
        • 1970-01-01
        • 2021-12-27
        相关资源
        最近更新 更多