首页 > 解决方案 > 从列数据库表中回显数组列表

问题描述

我正在尝试从列中回显所有行的列表userid并用逗号分隔它们并从最后一个值中删除最后一个逗号

    $sql_userid = "SELECT `userid` FROM `tabe_users`";
        $result_userid = $con->query($sql_userid);
        if ($result_userid->num_rows > 0) {
           while($row_userid = $result_push->fetch_assoc()) {
           $getallids = mysqli_free_result($result_userid) . ', ';
           $listallids = substr($getallids, 0, -2);
      }
    }

echo $listallids;

标签: phparraysecho

解决方案


用这个更新你的代码,你不能在 echo 中打印数组,如果你想在 echo 中打印,那么你必须在再次使用它解码时将数组编码为 json。

或者干脆使用print_r()

$sql_userid = "SELECT `userid` FROM `tabe_users`";
    $result_userid = $con->query($sql_userid);
    $listallids = array();
    if ($result_userid->num_rows > 0) {
       while($row_userid = $result_push->fetch_assoc()) {
       $getallids = mysqli_free_result($result_userid) . ', ';
       $listallids[] = substr($getallids, 0, -2);
  }
}

echo json_encode($listallids);
// or
print_r($listallids);

推荐阅读