我已经看了几个小时的相同代码,试图找出为什么我的查询不起作用。我下面列出的两个是不工作的两个。PDO查询不起作用
$getRequestIdQuery = "SELECT request_id
FROM request_table
WHERE request_key = '$requestKey'
AND sort_order = $so";
$getRequestId = $pdo->prepare($getRequestIdQuery);
$getRequestId->execute();
foreach($getRequestId as $idRow)
{
$requestId = $idRow['request_id'];
}
// This will update the ready status of the request id returned above
$updateReadyStatusQuery = "UPDATE request_table
SET request_ready = 1
WHERE request_id = $requestId";
$updateReadyStatus = $pdo->prepare($updateReadyStatusQuery);
$updateReadyStatus->execute();
上述情况在文件副本返回true时运行。我已经确定这是在运行,因为在每次测试运行期间都会显示上面的错误日志。我也确定有问题的查询工作正常,因为我在phpmyadmin中成功运行了查询(如错误日志中显示的那样)。以下是上面的代码只有这几行代码,可运行正常:
$checkForComposedQuery = "SELECT *
FROM composed_files
WHERE file_source_id = '$fsi'
AND file_number = '$fn'";
$checkForComposed = $pdo->prepare($checkForComposedQuery);
$checkForComposed->execute();
任何提示,以什么可能会导致这不行?如果有帮助,上述两个片段都在foreach循环中发生。
非常感谢提前。
UPDATE:
下结合的代码,其中包括由以下查尔斯补充建议:
$gotCopied = copy($sourceHymnFile, $destHymnFile);
if ($gotCopied == true) {
error_log("The file has been successfully copied.");
$idRow;
$getRequestIdQuery = "SELECT request_id
FROM request_table
WHERE request_key = ?
AND sort_order = ?";
$getRequestId = $pdo->prepare($getRequestIdQuery);
$getRequestId->execute(array($requestKey, $so));
error_log("this is the value of request key : ".$requestKey);
// Displays correct $requestKey value
error_log("This is the value of sort order : ".$so);
// Displays correct $so value
$idRow = $getRequestId->fetch(PDO::FETCH_ASSOC);
$requestId = $idRow['request_id'];
error_log("This is the value of the request id : ".$requestId);
// No output in error log for $requestId above
// This will update the ready status of the request id returned above
$updateReadyStatusQuery = "UPDATE request_table
SET ready = 1
WHERE request_id = ?";
error_log("This updates the status of the song request if the song is played : ".$updateReadyStatusQuery);
$updateReadyStatus = $pdo->prepare($updateReadyStatusQuery);
$updateReadyStatus->execute(array($requestId));
}
以下为进入正常运行正常常量:
if ($gotCopied == true) {
error_log("The file has been successfully copied.");
$idRow;
$getRequestIdQuery = "SELECT request_id
FROM request_table
WHERE request_key = ?
AND sort_order = ?";
$getRequestId = $pdo->prepare($getRequestIdQuery);
$getRequestId->execute(array(5, 2));
error_log("this is the value of request key : ".$requestKey);
error_log("This is the value of sort order : ".$so);
$idRow = $getRequestId->fetch(PDO::FETCH_ASSOC);
$requestId = $idRow['request_id'];
error_log("This is the value of the request id : ".$requestId);
// No output in error log for $requestId above
// This will update the ready status of the request id returned above
$updateReadyStatusQuery = "UPDATE request_table
SET ready = 1
WHERE request_id = ?";
error_log("This updates the status of the song request if the song is played : ".$updateReadyStatusQuery);
$updateReadyStatus = $pdo->prepare($updateReadyStatusQuery);
// This execute works correctly if a value is set for $requestId
$updateReadyStatus->execute(array($requestId));
}
你为什么没有在手动http://ru2.php.net/manual/en/pdo.query看着漂亮的样本.PHP? – zerkms 2011-03-10 01:54:40
我已经读过几次,但没有看到为什么第二个代码段工作,第一个没有。有什么我失踪? – TomC 2011-03-10 02:01:48
@ user652677:如果您不使用变量绑定:'pdo :: query()',则执行查询会更简单更好。用它来解决你的问题。在你的特定情况下,尽管我最好绑定'$ fsi'和'$ fn'变量。 – zerkms 2011-03-10 02:03:46