【问题标题】:To understand an error message from pg_last_error in PHP了解 PHP 中 pg_last_error 的错误消息
【发布时间】:2009-08-10 03:08:41
【问题描述】:

请参阅this answer to see the main problem


如何解决prepared statement中的以下错误信息?

我有一个 index.php,我通过许多处理程序将数据放入其中。以下错误消息出现在以下 URL 中,该 URL 是登录表单后的 URL。

http://localhost/codes/index.php?ask_question&email=masi.masi@gmail.com&passhash_md5=202cb962ac59075b964b07152d234b70

此问题基于this thread。我收到与 Daniel 类似的错误:

Warning: pg_prepare() [function.pg-prepare]: Query failed: ERROR: prepared statement "query11" already exists in /var/www/codes/handlers/handle_login_status.php on line 6
Prepared statement failed.

在代码中handle_login.php

$dbconn = pg_connect("host=localhost port=5432 dbname=masi user=masi password=123");
$result = pg_prepare($dbconn, "query11", "SELECT passhash_md5 FROM users
             WHERE email=$1;");
$passhash_md5 = pg_execute($dbconn, "query11", array($_POST['email']));

我按照丹尼尔的建议把handle_login.php改成了

 $dbconn = pg_connect("host=localhost port=5432 dbname=masi user=masi password=123");
 try{
     $result = pg_prepare($dbconn, "query11", "SELECT passhash_md5 FROM users
         WHERE email=$1;");
     if($result){
         $result->rayPrepared['query11'] = true;    // I changed $this to $result
     }else{
         throw new Exception('Prepared statement failed.');
     }
 }catch(Exception $e){
     echo $e->getMessage();
 }
 $passhash_md5 = pg_execute($dbconn, "query11", array($_POST['email']));

我仍然收到相同的错误消息。

【问题讨论】:

  • 你的 postgre 版本是什么? php 手册说这仅适用于 7.4 及更高版本。
  • @Zed:i486-pc-linux-gnu 上的 PostgreSQL 8.3.7,由 GCC gcc-4.3.real (Ubuntu 4.3.3-5ubuntu4) 4.3.3 编译
  • 我不知道发生了什么,但是在编写更多代码并删除旧垃圾之后,问题就消失了。 - 如果它回来,我会报告。 - 感谢您的回答!

标签: php try-catch


【解决方案1】:

是否准备好自己的实现?它真的会抛出异常吗?

试试这个

if(is_null($result)) {
 throw new Exception("No valid Result");
}  

编辑:
在第一条评论中得到答案

"Any error in the prepare is available from pg_last_error()."

pg_ last _ error(对不起,下划线创建草书格式)

【讨论】:

  • 如果你的意思是准备 pg_prepare,那么 pg_prepare 不是我自己的实现。它似乎没有抛出异常。我向我发出警告。
  • pg_last_error() 很有用。它给了我错误:准备好的语句“query11”已经存在。我将所有 query11 的名称更改为 query777,但 query777 出现相同的错误。
  • 啊,好吧,你的问题标题误导了:)
  • 我在 $handle_login.php 中删除了 $dbconn,因为我在 index.php 中也有它。我仍然得到同样的错误。
  • 查看 zed 对您的问题的评论。似乎很重要。
猜你喜欢
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 2013-05-03
相关资源
最近更新 更多