eHow Blog:

User-Submitted Article

How to Retrieve the Number of Rows in a MySQL Result Set Using PHP

There will come a time when you simply want to run a query just to see the number of rows being returned and not necessarily the data. For instance lets say you are trying to see if a username already exists in the database, if the result returned is greater than zero then you know that username has been taken.

Every programming language has a way to obtain this number and PHP is no different. Here is how you check the number of rows being returned from a MySQL database using PHP.

Difficulty: Easy
Instructions
  1. Step 1

    This tutorial assumes that you know how to connect to a MySQL database using PHP. If not, please see my other tutorials on this issue. I have very easy to follow step by step instructions to get you up to speed.

  2. Step 2

    First we setup our query that we need to excute. That is done with:
    "$RESULT=mysql_query("SELECT * FROM SOME_TABLE");"

    This line of code says run the select query and assign the results to the variable "$RESULT".

  3. Step 3

    We obtain the number of rows returned in that query by using the built in function "mysql_num_rows". That is written like this:
    $NUMBER_OF_ROWS = mysql_num_rows($RESULT);

    This says assign the number of rows returned from the result variable we created in step 2 in the variable "$NUMBER_OF_ROWS".

    Now we can use this variable to test for conditions in switch statements, if-else statements, loops and so on.

Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This

Computers Fans

Follow us

  • Computers
  • Computers
Get Free Computers Newsletters
eHow At Home
eHow At Home

Copyright © 1999-2010 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US † requires javascript

eHow Computers
eHow_eHow Technology and Electronics