Categories
MySQL PHP

PHP MySQL Count Rows

So, you are looking for a simple way to return the number of entries returned from a database query.

mysql_num_rows

This command counts all the rows currently defined by the database query and returns this value.  The best way to use this command, is while saving it the a variable.

Example

<?php

$link = mysql_connect(“localhost”, “mysql_user”, “mysql_password”);
mysql_select_db(“database”, $link);

$result = mysql_query(“SELECT * FROM table1”, $link);
$num_rows = mysql_num_rows($result);

echo “$num_rows Rows\n”;

?>

Now, if your database contained 12 entries, your output would look like this:
12 Rows
What do you think?