【问题标题】:Adding a postgresql the_geom function to a php insert将 postgresql the_geom 函数添加到 php 插入
【发布时间】:2013-10-25 15:31:08
【问题描述】:

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

这个脚本(完整版)是在John Boy Tutorial之后创建的

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

          ) 
        "; 

【问题讨论】:

    标签: php postgresql insert quotes


    【解决方案1】:

    为了帮助您解决引号放置问题,我建议使用prepared statements

    // 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 变量中存储了什么样的数据,但在这种情况下,你可以确定问题不会是引号,它会是什么在您的数据中。

    【讨论】:

      猜你喜欢
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 2013-04-15
      相关资源
      最近更新 更多