gpt4 book ai didi

php - 到达最后一行时如何回显不同的内容?

转载 作者:行者123 更新时间:2023-11-29 01:01:50 25 4
gpt4 key购买 nike

我不想回显最后一行后回显末尾的逗号。我怎样才能做到这一点?这是我的代码:

<?php
header("Content-type: application/json");
echo '{"points":[';

mysql_connect("localhost", "user", "password");
mysql_select_db("database");

$q = "SELECT venues.id, venues.lat, venues.lon, heat_indexes.temperature FROM venues, heat_indexes WHERE venues.id = heat_indexes.venue_id";

$res = mysql_query($q) or die(mysql_error());
while ($point = mysql_fetch_assoc($res)) {
echo $point['lat'] . "," . $point['lon'] . "," . $point['temperature'] . ",";
}
mysql_free_result($res);
echo ']}';
?>

最佳答案

你能不能用json_encode()相反,而不是手工制作 JSON?

$result = array();

//snip

while ($point = mysql_fetch_assoc($res)) {
$result[] = $point['lat'];
$result[] = $point['lon'];
$result[] = $point['temperature'];
}

//snip

header("Content-type: application/json");
echo json_encode( array('points' => $result) );

关于php - 到达最后一行时如何回显不同的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2653744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com