我需要从数据库中获取数据,但我需要这种形式的结果 {data,data,}

I need to fetch data from database but I need the result in this form of {data,data,}

<?php
  require_once 'includes/connection.php';
  $data = "select * from contacts_tbl";
  $query_posi = mysqli_query($con, $data);
  while($row = mysqli_fetch_array($query_posi)){
    echo $row['contact_number'];
  }
?>

结果是09277432079 09236677868

我要的结果是{09277432079,10236677868}

我在您的联系人数组上进行了循环,以演示在 while 循环中访问这些数据并回显联系人逗号分隔并括在括号中的概念:

https://onlinephp.io/c/6280d

$contacts = [
    '09277432079',
    '10236677868',
    '10436674963',
    ];

$arrayLength = count($contacts);
$i = 0;
echo '{';
while ($i < $arrayLength)
{
    echo $contacts[$i];
    if($i < $arrayLength-1)
        echo ',';
    $i++;
}
echo '}';

您需要创建一个以“{”作为初始值的变量,然后将迭代连接到您的变量。最后用“}”拼接。

<?php
  require_once 'includes/connection.php';
  $data = "select * from contacts_tbl";
  $query_posi = mysqli_query($con, $data);
  $result = "{";
  while($row = mysqli_fetch_array($query_posi)){
    $result.= $row['contact_number'].",";
  }
  //Delete the last comma
  $result = substr($result,0,1);
  $result.="}";
  echo $result;
?>