2
爲什麼第二個參數未傳遞給查詢?PHP函數不會將參數傳遞給PDO查詢
function LikesPastMonth($nMonth, $pid)
{
if($nMonth !== 0) {
$n = $nMonth*-1-1;
} else {
$n = -1;
}
$bdd = new PDO('mysql:host=localhost;dbname=xx', 'xx', 'xx', array(PDO::ATTR_PERSISTENT => true));
$req = $bdd->prepare('SELECT * FROM likes WHERE thedate = DATE_ADD(DATE_ADD(LAST_DAY(CURDATE()) , INTERVAL 1 DAY) , INTERVAL '.$n.' MONTH) AND pid = ?');
$req->execute(array($pid));
$result = $req->rowcount();
if(!$result) {
$result = 0;
}
return $result;
}
使用常數而不是arg而不準備查詢工作。
define('pid','777');
$req = $bdd->query('SELECT * FROM likes WHERE thedate = DATE_ADD(DATE_ADD(LAST_DAY(CURDATE()) , INTERVAL 1 DAY) , INTERVAL '.$n.' MONTH) AND pid = "'.pid.'"')->fetchColumn();
並直接將ARG到查詢不起作用(如何我曾嘗試2例):
1:
$req = $bdd->query('SELECT * FROM likes WHERE thedate = DATE_ADD(DATE_ADD(LAST_DAY(CURDATE()) , INTERVAL 1 DAY) , INTERVAL '.$n.' MONTH) AND pid = "'.$pid.'"')->fetchColumn();
2:
$req = $bdd->query('SELECT * FROM likes WHERE thedate = DATE_ADD(DATE_ADD(LAST_DAY(CURDATE()) , INTERVAL 1 DAY) , INTERVAL '.$n.' MONTH) AND pid = '.$pid.'')->fetchColumn();
如果把'$ pid'參數到查詢不無準備語句甚至工作,似乎'$ pid'說法有一個空值。如果'$ pid'爲空,則拋出一個異常,看看你是否以這種方式發現任何錯誤。 (另外,如果你只想獲得記錄數,可以使用'SELECT COUNT(*)'而不是實際選擇所有記錄。) – DCoder