我在 php 中有一个从服务器获取数据的脚本,但我想以 json 格式对其进行编码。
<?php
// attempt a connection
$dbh = pg_connect("host=10.22.35.11 dbname=iwmp_dev2 user=postgres ");
if (!$dbh) {
die("Error in connection: " . pg_last_error());
}
// execute query
//$sql = $_POST['pLat'];
$sql = "SELECT officer_name FROM iwmp_officer";
$result = pg_query($dbh, $sql);
if (!$result) {
die("Error in SQL query: " . pg_last_error());
}
$array = array();
while ($row = pg_fetch_assoc($result)) {
$i++;
$comm = implode(",",$row);
echo json_encode($comm);
}
// free memory
pg_free_result($result);
// close connection
pg_close($dbh);
?>
但我的输出是以格式出现的
"VM ARORA""Dr. CP REDDY""ARTI CHOWDHARY""JAGDISH SINGH" 当在 *pgsql_fetch_assoc* 上使用 implode func 时,不会出现"," (昏迷)。
请帮忙