将 postgresql the_geom 函数添加到 php 插入中


Adding a postgresql the_geom function to a php insert

我正在尝试将脚本从csv导入到post_gis中,所有内容都会导入,直到我添加the_geom列。我不知道在哪里放置st_geomfromtext('POINT( VARIABLE ,27700)')并且不断出现与引号有关的错误,所以我知道我在放置它们时做错了什么。

此脚本(完整版)是根据此John Boy教程创建的

 $sql ="INSERT INTO teams_tbl (team_id, name,  the_geom) VALUES 
            ( 
                '".addslashes($data[0])."', 
                '".addslashes($data[1])."', 
                'st_geomfromtext('POINT(".addslashes($data[2])."27700)')'
          ) 
        "; 

为了帮助您克服引号放置问题,我建议使用预处理语句:

// Prepare a query for execution
$result = pg_prepare(
    $dbconn,
    "my_query",
    'INSERT INTO teams_tbl (team_id, name, the_geom) '
    . 'VALUES ($1, $2, st_geomfromtext($3))');
// Prepare the string to be inserted as the_geom
$the_geom = "POINT(" . $data[2]. ", 27700)";
// Execute the prepared query using your variables.
// Note that it is not necessary to escape them
$result = pg_execute($dbconn, "my_query", $data[0], $data[1], $the_geom);

您可以稍后使用不同的参数执行相同的准备好的查询,这在循环中非常有用。

注意:我不知道您的$data变量中存储了什么样的数据,但在这种情况下,您可以确定问题不是引号,而是数据中的任何内容。