为什么第二个参数没有传递给查询?
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直接放入查询中并不起作用(我尝试过的两个例子):
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();
答案 0 :(得分:0)
在您的示例中,由于您将777封装在引号中,因此它可能无效。
你是否正在调用你的方法:
<?php
LikesPastMonth(3, 1);
?>