PDO在当前日期计算db中的记录

时间:2014-12-08 02:13:57

标签: php mysql pdo

我想计算新注册会员,但我遇到了麻烦

  $time = new DateTime();
    $currentdate = $time->format('Y-m-d');


    $cmd=$con->query("select count(*) from user where date(datereg) = ?");
    $cmd->execute($currentdate);
    $rowcount = $cmd->fetchColumn();

     echo $rowcount;

提前谢谢。

1 个答案:

答案 0 :(得分:1)

在您的查询中放置占位符是没有意义的,因为您没有准备它。

$cmd=$con->query("select count(*) from user where date(datereg) = ?");
         // ^ query not prepare

$cmd->execute($currentdate); // execute? $cmd is not prepared

只需在其中创建一个普通查询:

$sql = 'SELECT COUNT(*) AS `count` FROM user WHERE DATE(datereg) = CURDATE()';
$result = $con->query($sql);
$count = $result->fetchColumn();