tags:

views:

322

answers:

7

If I need to know the total number of rows in a table of database I do something like this:

$query = "SELECT * FROM tablename WHERE link='1';";
$result = mysql_query($query);
$count = mysql_num_rows($result);

Updated: I made a mistake, above is my actual way. I apologize to all

So you see the total number of data is recovered scanning through the entire database.

Is there a better way?

+9  A: 
SELECT COUNT(*) FROM tablename WHERE link='1';
webbiedave
Of course I'd replace * with one of the column names
Pedro
@Pedro - it might not make that much difference.
ChrisF
@Pedro et al, you should _not_ use a column name. That doesn't count NULL values in that column. Some people suggest using count(1) because it's faster, but that's rubbish in all but the most brain-dead DBMS'.
paxdiablo
@Pedro: (*) can result in slightly faster performance as MySQL will automatically use an index to get the result.
webbiedave
I stand corrected, and learn something new every day :)
Pedro
I have updated my question. Please see
Starx
+4  A: 

You could just do :

SELECT count(*) FROM tablename;

for your query. The result will be a single column containing the number of rows.

sheepsimulator
I have updated my question. Please see
Starx
+17  A: 
$query = "SELECT COUNT(*) FROM tablename WHERE link = '1'";
$result = mysql_query($query);
$count = mysql_result($result, 0);

This means you aren't transferring all your data between the database and PHP, which is obviously a huge waste of time and resources.

For what it's worth, your code wouldn't actually count the number of rows - it'd give you 2x the number of columns, as you're counting the number of items in an array representing a single row (and mysql_fetch_array gives you two entries in the array per column - one numerical and one for the column name)

Chris Smith
I have updated my question. Please see
Starx
@Starx The answer's still the same :)
Chris Smith
+3  A: 

If I need to know the total number of rows in a table of database

Maybe I'm missing something here but if you just want to get the total number of rows in a table you don't need a WHERE condition. Just do this:

SELECT COUNT(*) FROM tablename

With the WHERE condition you will only be counting the number of rows that meet this condition.

Mark Byers
+1 - helpful note on the WHERE condition.
sheepsimulator
I have updated my question. Please see
Starx
A: 

http://php.net/manual/en/function.mysql-num-rows.php You need this i think.

Arsheep
-1 Bad idea. This requires that you execute the full query. A `SELECT count(*)` is worlds more efficient.
Kenaniah
@Kenaniah: Not necessarily a bad idea. If all that the OP is looking for is the number of rows, then yes it is a bad idea. However, if the OP needs the data in other manipulations, then `mysql_num_rows()` might be better suited.
Joseph
@Joseph, agreed for other contexts. But in the context of *this* question, it's a bad idea.
Kenaniah
I have updated my question. Please see
Starx
A: 

If you are going to use the following SQL statement:

SELECT COUNT(*) FROM tablename WHERE link='1';

Make sure you have an index on the 'link' column

invarbrass
I have updated my question. Please see
Starx
A: 

use below code

$qry=SHOW TABLES FROM 'database_name';
$res=mysql_query($qry);
$output=array();
$i=0;
while($row=mysql_fetch_array($res,MYSQL_NUM)){
       ++$i;
       $sql=SELECT COUNT(*) FROM $row[0];
       $output[$i]=mysql_query($sql);
 }
$totalRows=array_sum($ouptput);
echo $totalRows;
diEcho
whwy downvoted??? atleast write some reason please.. so that i can update my knowledge
diEcho