抓住db2_prepare生成的警告 [英] Catch db2_prepare generated warning

查看:269
本文介绍了抓住db2_prepare生成的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DB2类有一个准备功能:

My DB2 class has a prepare function:

public function prepare()
{
    if (FALSE === ($this->stmt = db2_prepare($this->conn, $this->sql)))
    {
        throw new Exception($this->get_error());
    }

    return $this;
}

一些神秘的查询在php错误日志中引起警告:

Some mysterious query is causing a warning in the php error log:

[26-Jan-2012 11:17:32] PHP Warning:  db2_prepare(): Statement Prepare Failed in /<path to file>/Db2.php on line 178

db2_prepare不返回FALSE。否则,将抛出异常,我会得到一个回溯。

db2_prepare is not returning FALSE. Otherwise, an exception would be thrown, and I would get a backtrace.

我没有正确测试db2_prepare的返回值来捕获合法的错误/警告,还是db2_prepare有错误?

这是在i5 / iseries / as400上的Zend Server堆栈上运行。

This is running on the Zend Server stack on i5/iseries/as400.

在这种情况下,解决方法将是一个尝试catch块产生警告异常。不知道如何工作,这听起来像是另一个职位给我。

A workaround in this situation would be a try catch block that generates exceptions on warnings. Not sure how that would work, and that sounds like another post to me.

此外,@这里不是一个选项。它产生一个警告的原因。我不喜欢把头放在沙滩上。

Also, @ is not an option here. It's generating a warning for a reason. I prefer not to stick my head in the sand.

更新
我终于追踪了查询。这是一个格式错误的插入语句。即使准备失败并生成错误,返回了有效的语句资源,脚本继续生成异常。我正在使用exec来调用包含坏插件的脚本,但是我没有返回脚本的输出,这就是为什么我从未看到错误。

Update: I finally tracked down the query. It was a badly formed insert statement. Even though the prepare failed and generated an error, a valid statement resource was returned and the script continued with generating an exception. I was using exec to call the script that contained the bad INSERT, but I did not return the output of the script which is why I never saw the error.

这似乎对我来说太差了,但我没有时间进一步追求。

It still seems buggy to me, but I don't have time to pursue it further.

推荐答案

检查返回时,您可能需要比较 SQLSTATE ,而不是准备是否返回成功的资源。

When checking the return, you probably want to compare the SQLSTATE instead of whether the preparation returned a successful resource.

您可以使用 db2_stmt_error() ,这将给你上次执行的SQL状态(如果你传递一个特定的资源,最后一个执行该资源)。我相信,您从 PREPARE 返回的唯一SQLSTATE将是 00000 ,这是执行成功的代码。

You can retrieve the SQLSTATE in PHP using db2_stmt_error(), which will give you the SQL state of the last execution (if you pass a particular resource, the last execution on that resource). I believe the only SQLSTATE you want back from a PREPARE would be 00000, which is the code for a successful execution.

如果您想输出人为可读的SQL错误消息,可以使用 db2_stmt_errormsg() ,这将输出SQLSTATE和相应的错误消息字符串。

If you'd like to output a human-readable SQL Error Message, you can use db2_stmt_errormsg(), which will output the SQLSTATE and the corresponding error message string.

这篇关于抓住db2_prepare生成的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆