函数pg_query()在php中给出了错误

时间:2015-03-17 07:13:57

标签: php postgresql

我想在pgsql数据库中插入数据。但是当我想插入数据时,函数pg_query()会出错。错误是:

Warning: pg_query() [function.pg-query]: Query failed: ERROR: array value must start with "{" or dimension information LINE 1: INSERT INTO demo_insert(id, name) VALUES ('1234', 'abcd') ^ in C:\wamp\www\NetBeansProjects\PhpProject1\index1.php on line 19

我的代码如下:

<?php   

    require_once "connection.php"; 
   //page for connection to the database

    pg_set_client_encoding($dbconn, "UNICODE"); 
    //$dbconn is the variable where pg_connect() is executed

    $id="1234";

    $name="abcd";

    $query = "INSERT INTO demo_insert(id, name) VALUES ('$id', '$name')";  
    $result = pg_query($dbconn, $query);

    if($result)
    {
        echo "1 row is inserted";
    }
?>

你能告诉我这个错误的解决方案吗?

1 个答案:

答案 0 :(得分:2)

看起来你的一个列是数组类型,而不是“普通”类型。你能告诉我们你的桌子定义吗?

Offtopic:使用输入参数时,你必须做一些事情来避免SQL注入。最简单的解决方案是使用pg_query_params()而不是pg_query():

<?php
    $id="1234";

    $name="abcd";
    // placeholders $1 and $2:
    $query = "INSERT INTO demo_insert(id, name) VALUES ($1, $2)"; 
    // array with your content:
    $result = pg_query_params($dbconn, $query, array($id, $name)); 
?>