假设我有下表:
mysql> CREATE TABLE example (
-> `id` INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
-> `stuff` VARCHAR(255) NOT NULL
-> );
Query OK, 0 rows affected (0.06 sec)
stuff
列已声明为NOT NULL
。当在预准备语句中将null值传递给它时,它自然会失败。因此:
mysql> PREPARE test FROM "INSERT INTO example (stuff) VALUES (?)";
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> SET @a = NULL;
Query OK, 0 rows affected (0.00 sec)
mysql> EXECUTE test USING @a;
ERROR 1048 (23000): Column 'stuff' cannot be null
错误消息Column 'stuff' cannot be null
非常清楚。但是当我使用PDO的绑定参数执行完全相同的查询时,它不会给我错误消息。这是一个示例脚本:
<?php
$host = 'localhost';
$user = 'fakeuser';
$pass = 'fakepass';
$db_name = 'fakedb';
$dsn = "mysql:host=$host;port=3306;dbname=$db_name";
// Connect to the database.
try {
$DB = new PDO(
$dsn,
$user,
$pass,
array(PDO::ATTR_PERSISTENT => true)
);
} catch (PDOException $e) {
print "Unable to connect to database.\n";
exit;
}
// Make sure everything is handled in the UTF-8 character set.
$DB->exec('SET NAMES UTF8');
$SQL = 'INSERT INTO example (stuff) VALUES (:stuff)';
$query = $DB->prepare($SQL);
$values = array(
':stuff' => null,
);
$saved = $query->execute($values);
print "\$saved is:\n";
var_dump($saved);
print "\n\n";
print "Error code is:\n";
var_dump($DB->errorCode());
print "\n\n";
print "Error info is:";
var_dump($DB->errorInfo());
print "\n\n";
其中输出以下内容:
$ php test.php
$saved is:
bool(false)
Error code is:
string(5) "00000"
Error info is:array(3) {
[0]=>
string(5) "00000"
[1]=>
NULL
[2]=>
NULL
}
通过PHP回复给我的调试信息都不是很有用。我怎样才能让它给我真正的错误信息而不是这个00000
垃圾?
答案 0 :(得分:1)
请尝试$query->errorInfo()
。