简体   繁体   English

使用PostgreSql和PHP的INSERT语句中的错误

[英]Error in INSERT statement with PostgreSql and PHP

I'm using postgresql, with php and pdo. 我正在使用PostgreSQL和php和pdo。 I'm getting a error and I dont know what I'm doing wrong. 我遇到错误,我不知道自己在做什么错。

  • My environment is: 我的环境是:

PostgreSQL 9.3.6 PostgreSQL 9.3.6

Ubuntu 14.04.2 LTS Ubuntu 14.04.2 LTS

PHP 5.6.7 PHP 5.6.7

Apache/2.4.12 (Ubuntu) Apache / 2.4.12(Ubuntu)

The table which I'm using is this: 我正在使用的表是这样的:

-- Table: acesso.usuario

-- DROP TABLE acesso.usuario;

CREATE TABLE acesso.usuario
(
  usuarioidentificador serial NOT NULL,
  usuariopessoafisicaidentificador integer,
  usuarionome character varying(30) NOT NULL,
  usuarionomecompleto character varying(90) NOT NULL,
  usuariodescricao text,
  usuariosenha character varying(32) NOT NULL,
  usuariosenhaalterar bit(1) NOT NULL,
  usuariosituacao bit(1) NOT NULL
)
WITH (
  OIDS=FALSE
);

If I execute this at pgadmin3 如果我在pgadmin3上执行

INSERT INTO acesso.usuario(
            usuariopessoafisicaidentificador, usuarionome, 
            usuarionomecompleto, usuariodescricao, usuariosenha, usuariosenhaalterar, 
            usuariosituacao)
    VALUES (null, '', '', 
            null, '', '1', '1'
            );

I got this Query returned successfully: one row affected, 82 ms execution time. 我成功返回此查询:受影响的一行,执行时间为82毫秒。

So I'm trying the same with PHP: 所以我正在尝试使用PHP:

try {
            $query ="
            INSERT INTO acesso.usuario(
            usuariopessoafisicaidentificador, usuarionome, 
            usuarionomecompleto, usuariodescricao, usuariosenha, usuariosenhaalterar, 
            usuariosituacao)
    VALUES (null, '', '', 
            null, '', '1', '1'
            );
";

            $this->statement = $query;
            $conn = $this->getConnection();

            $query = $conn->prepare($this->statement, [\PDO::ATTR_CURSOR => \PDO::CURSOR_SCROLL]);

            $result = $query->execute();

        } catch (\Exception $e) {
            echo $e->getMessage() . "  <br> " . $e->getCode();

            var_dump($query);
            die();
            return false;
        }

I'm getting this output: 我得到以下输出:

SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "INSERT" LINE 2: INSERT INTO acesso.usuario( ^ 42601object(PDOStatement)#14 (1) { ["queryString"]=> string(291) " INSERT INTO acesso.usuario( usuariopessoafisicaidentificador, usuarionome, usuarionomecompleto, usuariodescricao, usuariosenha, usuariosenhaalterar, usuariosituacao) VALUES (null, '', '', null, '', '1', '1' ); " } SQLSTATE [42601]:语法错误:7错误:“ INSERT”处或附近的语法错误:第2行:INSERT INTO acesso.usuario(^ 42601object(PDOStatement)#14(1){[“” QueryString“] => string(291) ”插入acesso.usuario(usuariopessoafisicaidentificador,usuarionome,usuarionomecompleto,usuariodescricao,usuariosenha,usuariosenhaalterar,usuariosituacao)值(null,``,'',null,'','1','1'

I'm trying to find out what is happening. 我试图找出正在发生的事情。

Do you know it? 你知道吗?

Thank you anyway. 还是要谢谢你。

As the query is an INSERT (as opposed to a SELECT), it can't be mapped onto a cursor. 由于查询是INSERT(而不是SELECT),因此无法将其映射到游标。 The problem is in the attributes passed to prepare : 问题在于传递给prepare的属性:

 [\PDO::ATTR_CURSOR => \PDO::CURSOR_SCROLL]

Internally, PDO generates a query starting like this for postgres: 在内部,PDO会针对postgres生成如下查询:

DECLARE pdo_crsr_00000001 SCROLL CURSOR WITH HOLD FOR
  INSERT INTO acesso.usuario(...

and as documented in DECLARE , the query that follows must be a SELECT or VALUES command , which is why the parser yields a syntax error when it finds an INSERT there instead. 并且如DECLARE中所述,后面的查询必须是SELECT或VALUES命令 ,这就是为什么解析器在其中找到INSERT时会产生语法错误的原因。

The solution is simply to remove the second argument: 解决方案就是删除第二个参数:

$query = $conn->prepare($this->statement);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM