PHP/MySQL/PDO - 结果错误但没有数据库错误消息?
Posted
技术标签:
【中文标题】PHP/MySQL/PDO - 结果错误但没有数据库错误消息?【英文标题】:PHP/MySQL/PDO - results false but no DB error message? 【发布时间】:2011-09-17 11:28:15 【问题描述】:我有一大段代码执行得很奇怪。有时它起作用,有时它不起作用。服务器有一个绝对古老的 php 副本(5.1.6,五年前,但带有由 Red Hat 手动向后移植的安全补丁)。
这是代码,包括我目前在其中的调试行:
<?php
// Includes json_print, which does a json_encode, an
// appropriate content-type header, prints it, and exits the script.
include_once('json-functions.php');
$uid = $_POST['uid'];
$salted = false;
if(isset($_POST['salted'])) $salted = true;
// No uid given.
if(is_null($uid) || $uid === '')
$details = array(
'error' => 1,
'errorMessage' => 'No unique ID entered. Please try again.',
);
json_print($details);
// Validate uid. Must be 64 digit hexadecimal value.
$pattern = '/^[a-f0-9]64$/i';
if(preg_match($pattern, $uid) === 0)
$details = array(
'error' => 2,
'errorMessage' => 'Invalid unique ID.',
);
json_print($details);
include_once('../db.php');
header('Content-Type: text/plain');
// Is this salted already?
var_dump($salted);
// What's the UID?
var_dump($uid);
if(!$salted) $uid = hash('sha256', $salt.$uid);
// Was the UID salted? (shouldn't be double-salted)
var_dump($uid);
// The query
$SQL = 'SELECT ';
$SQL .= 'p.patronID AS patronID, ';
$SQL .= 'uniqueID, ';
$SQL .= 'status, ';
$SQL .= 'active, ';
$SQL .= 'd.name AS department, ';
$SQL .= 'docdelivery, ';
$SQL .= 'terms, ';
$SQL .= 'copyright, ';
$SQL .= 'lastLogin, ';
$SQL .= 'updated, ';
$SQL .= 'TIMESTAMPDIFF(MINUTE, lastLogin, NOW()) AS recency, ';
$SQL .= 'DATEDIFF(NOW(), updated) AS stale, ';
$SQL .= 'AES_DECRYPT(first, ?) AS first, ';
$SQL .= 'AES_DECRYPT(last, ?) AS last, ';
$SQL .= 'AES_DECRYPT(barcode, ?) AS barcode, ';
$SQL .= 'INET_NTOA(AES_DECRYPT(ip, ?)) AS ip, ';
$SQL .= 'AES_DECRYPT(email, ?) AS email, ';
$SQL .= 'AES_DECRYPT(phone, ?) AS phone, ';
$SQL .= 'AES_DECRYPT(address1, ?) AS address1, ';
$SQL .= 'AES_DECRYPT(address2, ?) AS address2, ';
$SQL .= 'AES_DECRYPT(city, ?) AS city, ';
$SQL .= 'AES_DECRYPT(state, ?) AS state, ';
$SQL .= 'AES_DECRYPT(zip, ?) AS zip ';
$SQL .= 'FROM patrons p, departments d ';
$SQL .= 'WHERE department = d.deptID ';
$SQL .= 'AND uniqueID = ?';
$query = $DB->prepare($SQL);
$p = array(
$key,
$key,
$key,
$key,
$key,
$key,
$key,
$key,
$key,
$key,
$key,
$uid,
);
$query->execute($p);
$result = $query->fetch();
// dump the results
var_dump($result);
print "\n\n";
// And any error info
var_dump($DB->errorInfo());
exit;
这是正常工作时的示例输出:
// Salted is true
bool(true)
// UID is:
string(64) "52223d99e1db275716028cf6fd4f58895b1df7eb8e061cefab346b8ce3cf4ff4"
// It was not double-salted:
string(64) "52223d99e1db275716028cf6fd4f58895b1df7eb8e061cefab346b8ce3cf4ff4"
// Results were:
array(46)
["patronID"]=>
string(1) "126"
[0]=>
string(1) "126"
["uniqueID"]=>
string(64) "52223d99e1db275716028cf6fd4f58895b1df7eb8e061cefab346b8ce3cf4ff4"
[1]=>
string(64) "52223d99e1db275716028cf6fd4f58895b1df7eb8e061cefab346b8ce3cf4ff4"
["status"]=>
string(1) "4"
[2]=>
string(1) "4"
["active"]=>
string(1) "1"
[3]=>
string(1) "1"
*** snip! ***
[21]=>
string(2) "TX"
["zip"]=>
string(5) "78623"
[22]=>
string(5) "78623"
// Errors reported?
array(1)
[0]=>
string(5) "00000"
这是失败时的示例输出:
// Salted is true
bool(true)
// UID is:
string(64) "1d6fa3b897b07301a836f5441d23f60e7cb4b52a00ee6d20648fe51b01c769bf"
// It was not double salted
string(64) "1d6fa3b897b07301a836f5441d23f60e7cb4b52a00ee6d20648fe51b01c769bf"
// Results were:
bool(false)
// Error code was:
array(1)
[0]=>
string(5) "00000"
我不明白为什么它适用于某些 uid,但不适用于其他 uid。此外,在第二个示例中,结果集出现 FALSE,但数据库报告错误 00000,这意味着“没有错误”。我已经检查了查询是否准备妥当,在这两种情况下都是。
我在这里错过了什么?
【问题讨论】:
var_dump($query->error)
在发生故障的地方显示什么?
var_dump($query->error)
为空。也许你在想 $query->errorInfo(),它写着array(1) [0]=> string(5) "00000"
。
【参考方案1】:
尝试直接在数据库上运行查询并检查是否正常。
【讨论】:
我将此标记为答案,因为运行几个测试查询最终让我弄清楚了。我运行的第一个确实有效,因此我的最后一条评论 - 直到后来我用另一个 UID 尝试它时我才发现它。尽管如此,这还是成功了。 (...对于那些正在寻找的人,解决方案在下面)。【参考方案2】:*手掌*
好的,我想通了。查询因此失败:
WHERE department = d.deptID
没有工作的帐户返回零结果,因为他们的部门 ID 在部门表中没有对应部门。
对不起,我的烤箱里有一份不错的乌鸦。还是谢谢!
【讨论】:
噢!哦,恭喜你搞清楚了。【参考方案3】:执行后检查$query->errorInfo()。
【讨论】:
在查询执行后立即运行时,$query->errorInfo() 说:array(1) [0]=> string(5) "00000"
。遗憾的是,这并没有揭示出任何谜团。以上是关于PHP/MySQL/PDO - 结果错误但没有数据库错误消息?的主要内容,如果未能解决你的问题,请参考以下文章
Apache,PHP,MySQL,PDO,权限被拒绝,laravel