我已經看了幾個小時的相同代碼,試圖找出爲什麼我的查詢不起作用。我下面列出的兩個是不工作的兩個。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