【发布时间】:2021-09-12 22:34:40
【问题描述】:
我的 PHP 文件成功连接到 MSQL 数据库并查询 3 个表中的所有列。
$sql = "SELECT * FROM facilitator, address, certification WHERE certification.ID=10002 LIMIT 0,3";
对于本示例,我将输出限制为 3 行。
但我的数组正在生成多组相同用户的数据。它似乎没有遍历数据库的行。这是 PHP 数组
// Create array
$geojson = array('type' => 'FeatureCollection', 'features' => array());
// Loop each row in database
while($row = mysqli_fetch_assoc($result)) {
$marker = array(
'type' => 'Feature',
'properties' => array(
'Name' => $row['First_Name'],
),
'geometry' => array(
'type' => 'Point',
'coordinates' => array(
$row["Longitude"], $row["Latitude"]
)
)
);
array_push($geojson['features'], $marker);
}
$json_string = json_encode($geojson, JSON_PRETTY_PRINT);
echo "<pre>".$json_string."<pre/>";
以及 GEOJSON 结果(限 3 个用户)
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"Name": "Luz"
},
"geometry": {
"type": "Point",
"coordinates": [
"175.298090",
"-37.791458"
]
}
},
{
"type": "Feature",
"properties": {
"Name": "Luz"
},
"geometry": {
"type": "Point",
"coordinates": [
"175.298090",
"-37.791458"
]
}
},
{
"type": "Feature",
"properties": {
"Name": "Luz"
},
"geometry": {
"type": "Point",
"coordinates": [
"175.298090",
"-37.791458"
]
}
}
]
}
您可以看到“Luz”数据每次都重复。你能帮我解决数组不正确的地方吗?谢谢。
【问题讨论】: