社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

PHP通过逗号从mysql中提取geojson

Jonas • 3 年前 • 1397 次点击  

下面是我的php和geojson的样子。我想用逗号将它们分开,除了最后一个“while query”,因为它与传单一起工作。我试着在$data['geojson']后面回显一个逗号,但它在结尾也回显一个逗号,这使它无法工作。现在只要数据库中只有一行,它就可以工作。

geojson存储在geojson列中,每个geojson行都有自己的行

    $sql = "SELECT * FROM `polygons` ORDER BY id";

    $result = mysqli_query($dbcon, $sql);

    $check = mysqli_num_rows($result);
    if($check > 0){
        echo '{"type":"FeatureCollection","features":[';
        
    while($data= mysqli_fetch_assoc($result)){
    
    echo $data["geojson"];
    
    
    }
    echo "]}";


GEOJSON:

{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[9.836037,54.92779],[9.79818,54.93067],[9.769213,54.924124],[9.745858,54.906048],[9.796566,54.879997],[9.860286,54.89273],[9.837761,54.925973],[9.836037,54.92779]]]}}


{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[9.855609,54.956418],[9.867858,54.923851],[9.971178,54.943613],[9.92426,54.969291],[9.855609,54.956418]]]}}]}

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128922
 
1397 次点击  
文章 [ 1 ]  |  最新文章 3 年前
Barmar
Reply   •   1 楼
Barmar    3 年前

不要通过连接或回显字符串来创建JSON。创建一个包含所有数据的数组,然后使用 json_encode() .

因为表列包含 JSON ,在将其推送到结果数组之前,需要对其进行解码。否则它将被编码两次。

$sql = "SELECT * FROM `polygons` ORDER BY id";

$result = mysqli_query($dbcon, $sql);

$check = mysqli_num_rows($result);
if($check > 0){
    $array = ['type' => 'FeatureCollection', 'features' => []];
    while($data= mysqli_fetch_assoc($result)){
        $array['features'][] = json_decode($data['geojson'], true);
    }
    echo json_encode($array);
}