-2

I simply want to count the total number of rows I have in a table.

I will put this number in parenthesis at the right of the categories names in my website's sidebar.

1
  • 5
    Check out the right hand column/sidebar of this page. It's a related questions list. You are likely to find the answer to your question there. Because it is very simple. Commented Aug 1, 2012 at 0:21

2 Answers 2

5

In SQL

  SELECT COUNT(*) FROM your_table

And from PHP

  $result = mysql_query('SELECT COUNT(*) AS count FROM your_table');
  $row = mysql_fetch_assoc($result);
  $count = $row['count']; 

Alternatively if you were to use PDO (without a prepared statement)

  $count = (int) $pdo->query('SELECT COUNT(*) FROM your_table')->fetchColumn(); 

Then simply echo it

  echo '(Rows: ' . $count . ')';

Or if you want to be fancy using printf

  printf('(Rows: %d)', $count);
1

It's also possible to do this from PHP in another way using mysql_num_rows() function.

Example:

$result = mysql_query('SELECT id FROM your_table');
$count = mysql_num_rows($result);
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.