本文介绍了检查语句错误pdo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此构造来检查为什么不执行语句的错误:

i am using this construct for checking the error why a statement is not executed:

$value1 = $username;
$value2 = $firstname;
$value3 = $lastname;

$sql = "INSERT INTO table (row1, row2, row3) VALUES (?, ? , ?)";

try {
    $stmt = $dbh->prepare($sql);

    $stmt->bindParam(1, $value1);
    $stmt->bindParam(2, $value2);
    $stmt->bindParam(3, $value3);

    $stmt->execute();
} catch(PDOException $e) {
    $var .= $e->getMessage();
}

include 'log.php';

log.php:

$logfile = fopen("logfile.txt", "a");
$error = date("d.m.Y H:i:s")." - ".$var.".\r\n";
fwrite($logfile, $error);
fclose($logfile);

我的数据库中没有插入,并且我的log.php也为空,所以我无法保存该错误,有人可以帮助我吗?问候

there is no insert in my database and my log.php is empty as well, so i cant save the error, anybody could help me? greetings

推荐答案

我不知道为什么PDO错误吸引了PHP用户如此多的关注.您将永远不会看到如此精心的包含或图像处理.但是由于某些原因,PDO语句始终会受到特殊处理.

I have no idea why PDO errors attracts so distinct attention of PHP users. You would never see an include or an image manipulation so much nursed. But PDO statements, for some reason, always receive a special treatment.

尽管绝对没有PDO错误的特殊之处.这与任何其他错误完全相同,并且不需要以任何特殊方式处理PDO错误.如果找不到文件-PHP将记录该错误.如果PDO查询失败- PHP也会记录该错误.只是告诉PHP这样做.

While there is absolutely nothing special with PDO errors. That's exactly the same error as any other, and there is no need to treat PDO errors whatever special way. If a file was not found - PHP will log that error. If PDO query fails - PHP will log that error as well. Just tell PHP to do so.

ini_set('log_errors', 1);

是您所需要的.然后运行您的代码并记录错误

is all you need. Then run your code and have an error logged

$sql = "INSERT INTO table (row1, row2, row3) VALUES (?, ? , ?)";
$stmt = $dbh->prepare($sql);
$stmt->execute(array($username, $firstname, $lastname));

这篇关于检查语句错误pdo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:01
查看更多