This is another question, which, when mistakenly posed, gives rise to many terrible decisions, all complicates the task of solving a nonexistent problem.
An extremely simple and obvious rule for any interaction with the database is
Always select only the data you need.
From this point of view, the question is incorrect, and the accepted answer is right. But the other proposed solutions are just awful.
The question is, "how to get the bill wrong." You should not answer it simply, but instead, the only correct answer is: "You should never select the rows to count them. Instead, ALWAYS ask the database to count the rows for you." This rule is so obvious that it's incredible to see how many are trying to break it.
Having studied this rule, we will see that this is an SQL issue, not even related to PDO. And, if asked correctly, from the point of view of SQL, the answer would appear instantly - DISTINCT .
$num = $db->query('SELECT count(distinct boele) FROM tbl WHERE oele = 2')->fetchColumn();
- The correct answer to this specific question.
The own decision to open the poster is also acceptable from the point of view of the aforementioned rule, but will be less effective in general terms.
Your Common Sense Oct 13 '15 at 5:54 2015-10-13 05:54
source share