pgsql查询结果用逗号分隔

时间:2018-08-19 03:30:13

标签: php postgresql

我的数据库 表//名称:帐户

id | login | password | macaddress
1  | ab    | ex       | 2X:D0:5X:5E:77:CX
2  | ac    | example  | 2X:D0:5X:5E:77:CX
3  | ad    | example  | 5X:Y0:GX:FE:27:G8

查询

$sql = pg_query("SELECT * FROM accounts where macaddress = '2X:D0:5X:5E:77:CX' ")
while( $row = pg_fetch_assoc($sql) )
echo "$row[id]";

结果:1​​2

我想要的结果是:1,2

我希望你能帮助我解决这个问题。 谢谢

1 个答案:

答案 0 :(得分:1)

首先将值放入数组中并加入它们。

$array = [];

$sql = pg_query("SELECT * FROM accounts where macaddress = '2X:D0:5X:5E:77:CX' ")
while( $row = pg_fetch_assoc($sql) )
$array[] = $row[id];

echo join(',', $array);