【问题标题】:How to get values from database and put it in a certain bracket in a php file that is json_encoded? [duplicate]如何从数据库中获取值并将其放在 json_encode 的 php 文件中的某个括号中? [复制]
【发布时间】:2020-03-14 19:29:29
【问题描述】:

这是我的代码:

<?php 

session_start();

$con = mysqli_connect('localhost', 'id128XXXXX', 'LXXXX');

mysqli_select_db($con, 'idXXXXXX');

$query = "select * from sensor order by id desc limit 1";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result);


$value = array( 
    "geometry"=>array( 
        "type"=> "Point", 
        "coordinates"=> [0,0] ///I want to get values from a database and put it in this bracket.
    ) , 

    "type"=>"Feature", 
    "properties" => (object) array()
); 

// Use json_encode() function 
$json = json_encode($value); 

// Display the output 
echo($json); 

?> 

对此的任何帮助将不胜感激,谢谢。至于我试过的。我尝试使用

 "coordinates"=> [$row[longitude],$row[lat]]

最终给了我这种类型的输出:

{"geometry":{"type":"Point","coordinates":["0","0"]},"type":"Feature","properties":{}} 

我需要 ["0","0"] 为 [0,0],因为添加了引号后,地图标记将不会显示。如果有人问我正在使用 mapbox,我需要输出此 json 数据以供地图标记保持刷新。

【问题讨论】:

标签: php json mapbox geojson


【解决方案1】:

将结果转换为整数:

"coordinates"=> [intval($row['longitude']), intval($row['lat'])]

别忘了引用字符串longitudelat

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 2011-11-08
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多