使用php将数据插入postgresql时出错

时间:2015-07-15 14:42:13

标签: php postgresql

我试图将一些数据插入PostgreSQL数据库的表中。我为此目的使用预备语句,如:

$db = new PDO('pgsql:dbname=wind;user=postgres;password=1249;host=localhost;port=5432');
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

for ($x = 0; $x < sizeof($arrayAddress); $x++) {
   $stmt = $db->prepare("INSERT INTO geocoding_test (address,googlemaps) VALUES (address=:address, sucUn=:sucUn ) ");
   $stmt->execute(array(address => $arrayAddress[$x], sucUn => $arraySucUnsuc[$x] ));
} 

...其中arrayAddress是一个包含所有地址的数组,arraySucUnsuc是一个包含一些字符串的数组。

当我执行此操作时,我收到以下错误:

Fatal error: Uncaught exception 'PDOException' with message 
    SQLSTATE[42703]: Undefined column: 7 ERROR: column 'address' does not exist 
    LINE 1: ... INTO geocoding_test (address,googlemaps) VALUES (address=$1... ^
    HINT: There is a column named 'address' in table 'geocoding_test', but it cannot be
    referenced from this part of the query.' in C:\MAMP\htdocs\Wind\predictVdslEligibilityWSLog-20150614\myWebCrawler.php:98
    Stack trace:
    #0 C:\MAMP\htdocs\Wind\predictVdslEligibilityWSLog-20150614\myWebCrawler.php(98): PDOStatement-&gt;execute(Array)
    #1 {main} thrown in C:\MAMP\htdocs\Wind\predictVdslEligibilityWSLog-20150614\myWebCrawler.php on line98

我做错了什么?

这也是我在数据库中的表的定义:

CREATE TABLE geocoding_test
(
    id serial NOT NULL,
    address character varying(600),
    googlemaps character varying(50)
)

1 个答案:

答案 0 :(得分:4)

插入语法为:

INSERT INTO tablename (fieldname1, ...) VALUES (value1, ...)

你有

... VALUES (address=:address, ...

您尝试对正在传递的参数比较(=)现有值address。由于这是INSERT查询,因此address1值尚不存在:您无法从不存在的内容中读取。

查询应该只是

.... VALUES(:address, :sucUn)