【发布时间】:2013-05-13 21:21:56
【问题描述】:
public function votesThisMonth()
{
$this->query = $this->pdo->prepare
("
SELECT
COUNT(*) AS num_votes
FROM
votes
WHERE
YEAR(year) = YEAR(CURRENT_DATE)
AND MONTH(month) = MONTH(CURRENT_DATE)
");
$result = $this->query->execute();
return $result['num_votes'];
}
我想返回 2013 年第 5 个月的所有行。 但是当我有 10 多行具有相同数据时它返回“1”?
我做错了什么?
这些是位于我的数据库中的行:
id ip year month
1 127.0.0.1 2013 5
2 127.0.0.1 2013 5
3 127.0.0.1 2013 5
4 127.0.0.1 2013 5
5 127.0.0.1 2013 5
6 127.0.0.1 2013 5
7 127.0.0.1 2013 5
8 127.0.0.1 2013 5
9 127.0.0.1 2013 5
10 127.0.0.1 2013 5
11 127.0.0.1 2013 5
12 127.0.0.1 2013 5
13 127.0.0.1 2013 5
14 127.0.0.1 2013 5
15 127.0.0.1 2013 5
编辑了我的方法,请看:
public function votesThisMonth()
{
$this->query = $this->pdo->prepare
("
SELECT
*
FROM
votes
WHERE
YEAR(year) = YEAR(CURRENT_DATE)
AND MONTH(month) = MONTH(CURRENT_DATE)
");
$this->query->execute();
return $this->query->rowCount();
}
返回 '0',为什么?
【问题讨论】:
-
我在编辑中添加了我的表格外观
-
count($result->fetchAll())。返回获取的所有行。如果您选择一列而不是 count(*),这将起作用。
-
已编辑,看看我的新方法
-
获取查询,并在 mysql 中手动运行它,看看你得到了什么