Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数pg_query()在php中给出了一个错误_Php_Postgresql - Fatal编程技术网

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

函数pg_query()在php中给出了一个错误,php,postgresql,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

我想在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";
    }
?>

看起来您的一列是数组类型,而不是“普通”类型。你能给我们看看你的表格定义吗

Offtopic:在使用输入参数时,必须采取措施避免SQL注入。最简单的解决方案是使用pg_query_params()而不是pg_query():



id是什么类型的
column?您能带上
demo\u insert
表格的说明吗?谢谢Frank先生的回复。但它也给出了同样的错误。警告:pg_query_params()[function.pg query params]:查询失败:错误:数组值必须以“{”开头或第21行C:\wamp\www\NetBeansProjects\PhpProject1\index1.php中的维度信息………我的表定义是:创建表demo_insert(id字符变化(10)[],名称字符变化(50)[]),带(OIDS=FALSE);ALTER TABLE demo_insert OWNER TO postgres;非常感谢大家。事实上,我的数据类型是数组类型。因此,我得到了错误。更正后,我得到了结果…字符变化(10)[]:[]表示您需要数组,而不是“普通”数据类型。删除该表并创建一个没有[and]的新表.为什么要将VARCHAR用作数字id?请使用整数。
<?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)); 
?>