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?

Comments

2 responses to “PHP MySQL Count Rows”

  1. Theresa Gavles Avatar

    Greetings, I like your blog very much. It has good info in it. I came here from search engine yahoo while was searching for website hosting info. Keep working on blog I d place bookmark, thanks.

  2. Gregoria Mahraun Avatar

    Thank you a great deal for composing like an helpful write-up on this topic. This has seriously designed me feel and I hope to browse additional.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.