如何从 PDO 中挤出错误消息?

发布于 2024-09-24 01:25:55 字数 611 浏览 3 评论 0原文

我似乎无法从 PDO 收到任何错误消息:

#$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
try {
  $sth = $dbh->prepare('@$%T$!!!');
  print_r($sth);
  print_r($dbh->errorInfo());
} catch (PDOException $e) {
    echo $e->getMessage();
}

它仅给出:

PDOStatement Object
(
    [queryString] => @$%T$!!!
)
Array
(
    [0] => 00000
    [1] =>
    [2] =>
)

setAttribute 没有任何帮助。

这是 PHP 5.3.3 Apache 2.0 处理程序
已启用 MySQL 的 PDO 驱动程序
客户端 API 版本 mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $

我可以做什么来获取错误信息?

I can't seem to get any error message from PDO:

#$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
try {
  $sth = $dbh->prepare('@$%T$!!!');
  print_r($sth);
  print_r($dbh->errorInfo());
} catch (PDOException $e) {
    echo $e->getMessage();
}

It is giving out only:

PDOStatement Object
(
    [queryString] => @$%T$!!!
)
Array
(
    [0] => 00000
    [1] =>
    [2] =>
)

setAttribute doesn't help anything.

It's PHP 5.3.3 Apache 2.0 Handler
PDO Driver for MySQL enabled
Client API version mysqlnd 5.0.7-dev - 091210 - $Revision: 300533 $

What can I do to get error information?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

子栖 2024-10-01 01:25:55

setAttribute导致 PDO 抛出错误或异常 - 最新的当您执行查询时。

对于模拟的准备好的语句,prepare() 中没有检查:

模拟准备好的语句不与数据库服务器通信,因此 PDO::prepare() 不会检查该语句。

但是当查询发送到服务器时,execute() 中将会有一个。

但是,自从 mySQL 4.1 起,mySQL 驱动程序就支持本机准备好的语句,因此这不适用。使用

$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

必须导致您使用的查询出现异常。

setAttribute will cause PDO to throw up errors or exceptions - the latest when you execute the query.

For emulated prepared statements, there is no check in prepare():

Emulated prepared statements does not communicate with the database server so PDO::prepare() does not check the statement.

But there will be one in execute() when the query gets sent to the server.

However, the mySQL driver supports native prepared statements since mySQL 4.1 anyway, so this shouldn't apply. Using

$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

must cause an exception for the query you use.

忘东忘西忘不掉你 2024-10-01 01:25:55

我也试图在数据库句柄级别从 errorInfo() 获取信息,但最终我使用 PDOStatement::errorInfo() 从语句级别获取信息

每个 PHP 网站:

PDO::errorInfo() 仅检索直接在数据库句柄上执行的操作的错误信息。如果通过 PDO::prepare() 或 PDO::query() 创建 PDOStatement 对象并在语句句柄上调用错误,PDO::errorInfo() 将不会从语句句柄反映错误。您必须调用 PDOStatement::errorInfo() 来返回在特定语句句柄上执行的操作的错误信息。

I too was trying to get the information from errorInfo() at the database handle level, but I ended up getting the information from the statement level with PDOStatement::errorInfo()

Per PHP web site:

PDO::errorInfo() only retrieves error information for operations performed directly on the database handle. If you create a PDOStatement object through PDO::prepare() or PDO::query() and invoke an error on the statement handle, PDO::errorInfo() will not reflect the error from the statement handle. You must call PDOStatement::errorInfo() to return the error information for an operation performed on a particular statement handle.

亣腦蒛氧 2024-10-01 01:25:55

这将打印错误代码及其相应的详细消息。

建议:这只是一个演示。仅用于调试目的。不要在发布版本中向公众显示错误消息。

try{
connection=$this->get_connection();//here i brought my connection string
connection->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
/**
Do your works here..
//$statement=$connection->prepare($sql);
//if you are using errorInfo use after prepare statement before execute.here in this method i am not using it.
//print_r($statement->errorInfo());
**/

$statement->execute();
}
catch(PDOException $e) {
              //this will echo error code with detail
              //example: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'nasme' in 'field list'
              echo $e->getMessage();
            }
//$statement=null;

This will print error code as well its corresponding detailed message.

Advice: this is just a demonstration. Just use for debugging purpose. Don't enable to show error messages to the public in a release version.

try{
connection=$this->get_connection();//here i brought my connection string
connection->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
/**
Do your works here..
//$statement=$connection->prepare($sql);
//if you are using errorInfo use after prepare statement before execute.here in this method i am not using it.
//print_r($statement->errorInfo());
**/

$statement->execute();
}
catch(PDOException $e) {
              //this will echo error code with detail
              //example: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'nasme' in 'field list'
              echo $e->getMessage();
            }
//$statement=null;
黑白记忆 2024-10-01 01:25:55

您需要首先执行查询,然后检查错误:所以这样做:

 $sth->execute();

然后检查错误。然后你会得到错误(如果有的话)。

You need to first execute the query and then check for errors: So do it like this:

 $sth->execute();

and then check for errors. Then you will get errors, if any.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文