代码/信息
我有这个查询:
$stmt = $dbh->prepare("INSERT INTO `tap_sajobs` (
`ja_jid`,
`ja_reference`,
`ja_datePosted`,
`ja_dateUpdated`,
`ja_title`,
`ja_summary`,
`ja_description`,
`ja_location`,
`ja_email`,
`ja_url`,
`ja_salaryperiod`,
`ja_salarymin`,
`ja_salarymax`
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
这些值由数组$ja
填充。 我可以用这个来模拟查询:
$query = "INSERT INTO `tap_sajobs` (`ja_jid`,`ja_reference`,`ja_datePosted`,`ja_dateUpdated`,`ja_title`,`ja_summary`,`ja_description`,`ja_location`,`ja_email`,`ja_url`,`ja_salaryperiod`,`ja_salarymin`,`ja_salarymax`)
VALUES (".implode(",",$ja).");";
echo $query;
输出如下:
INSERT INTO `tap_sajobs` (`ja_jid`,`ja_reference`,`ja_datePosted`,`ja_dateUpdated`,`ja_title`,`ja_summary`,`ja_description`,`ja_location`,`ja_email`,`ja_url`,`ja_salaryperiod`,`ja_salarymin`,`ja_salarymax`)
VALUES ('2049216', '2091046', '2015-07-23', '2015-08-20T06:49:30Z', 'Consultant', 'Will consider a graduate or a fully qualified fellow.', 'Test', 'No Location Set', 'test@foo.com', 'https://test.foo.com/3207/2049216/vh6zpeigfx6edggjrhshwulvvy?site=live', '0', '0', '0')
执行查询的实际代码是:
if(!$stmt->execute($ja)) {
print_r($stmt->errorInfo());
print_r($dbh->errorInfo());
exit("Error communicating with database.");
}
问题
通过代码执行查询时,我看到错误“与数据库通信时出错”和 $dbh->errorInfo()
输出:
Array ( [0] => 00000 [1] => [2] => ) 1
但是,将 PHP 代码输出的查询直接执行到 phpMyAdmin 中不会出现问题。
此问题的根源是什么,或者如何找到它?
最佳答案
SQLSTATE
代码00000
告诉您最后执行的语句没有发生错误。如果没有行受到 SQL 查询的影响,以下行将返回 0
。
$stmt->execute($ja);
因此,即使没有错误,您也会进入 if
block ,因为 !$stmt->execute($ja)
将等于 1
。因此,为了捕获错误,我认为最好使用,
$result = $stmt->execute($ja);
if($result == false) {
// put the error handling logic here.
}
关于php - 无法调试失败的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32112707/