3

I would like to get the count of the mysql query results in php.

The query usually returns up to 10 rows.

Which one in the below is better to use:

1.

$query = "SELECT * FROM test_table WHERE test_no=12345";
$queryResult = $db->query($query);
$count = $queryResult->size();
if($count < 5){}

2.

$query = "SELECT COUNT(test) AS count FROM test_table WHERE test_no=12345";
$queryResult = $db->query($query);
$result = $queryResult->fetch();
if($result['count'] < 5){}

Also, please let me know if there is another better way to use it

1
  • I'd use the second one, no use getting the data if all you're doing is counting indices. Commented Dec 7, 2015 at 23:39

3 Answers 3

2
$query = 'SELECT COUNT(1) FROM test_table WHERE test_no=12345';
$queryResult = $db->query($query);
$count = $queryResult->fetchColumn();

This is the best way, assuming you are using PDO. Do not specify a column name in COUNT(), use * or 1. Then use fetchColumn() to get the first column's data.

Sign up to request clarification or add additional context in comments.

Comments

1

It's probably better to let MySQL handle row counting. This is more important when large amounts of data are in play. If PHP were in charge of counting the rows, all of that data would need to be sent from MySQL to PHP, only for PHP to then count & discard it. It's better to skip the middle man. So the SELECT COUNT(test) query is the preferable one, of the two choices you provided.

5 Comments

is it better to use SELECT COUNT(column_name) instead of SELECT COUNT(*) ?
That shouldn't make a difference, but since it's a best practice to avoid using wildcards in SQL queries, I'd opt for the former. Also, apologies for introducing that confusion with my answer. I edited it to state SELECT COUNT(test)
@Jeff, no you should not use a column name for this approach. This is causing extra work for SQL to check against the expression you provide, if you're just trying to count rows, you should always use * or 1 which evaluate to the same thing.
Is the extra work essentially "verify that the referenced column name exists", or is it more significant than that?
@NateB, good question. I don't know the specifics regarding using a column name in there, but a column name isn't what the expression is supposed to be used for. MySQL doesn't have very much documentation on the count() function, from my experience, it's usually never a good idea to put something in the count() function unless you need a distinct call.
0

If you're using PDO, the best way is to use PDO's rowCount() method. This methods returns the number of rows returned by the query. Here's an example.

try
{
$s = $conn->execute("YOUR QUERY HERE");
$count = $s->rowCount();
}
catch(PDOException $e)
{
echo $e->getMessage();
}

1 Comment

This is inefficient for large results as you're actually getting all the data from MySQL only to count the number of rows. It's much more efficient to use the SQL count() function. It's also important to note, while rowCount() works for MySQL selects, it does not work for all databases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.