quickly get number of rows in a table
Moderator: General Moderators
- Sevengraff
- Forum Contributor
- Posts: 232
- Joined: Thu Apr 25, 2002 9:34 pm
- Location: California USA
- Contact:
quickly get number of rows in a table
I looked in the php manual, and there didn't seem to be a function for this.
The way I do it is to have a query that will run throught all the rows
$sql = "SELECT * FROM USERS";
and then use mysql_affected_rows as my count.
but this seems like an inefficent way of doing it. how should i do it?
The way I do it is to have a query that will run throught all the rows
$sql = "SELECT * FROM USERS";
and then use mysql_affected_rows as my count.
but this seems like an inefficent way of doing it. how should i do it?
- Sevengraff
- Forum Contributor
- Posts: 232
- Joined: Thu Apr 25, 2002 9:34 pm
- Location: California USA
- Contact:
You can improve performance slightly if you know of a column in your table that has a value (is non-null) for every row. Then you can use:
Code: Select all
select count(column_name) from tablename- Sevengraff
- Forum Contributor
- Posts: 232
- Joined: Thu Apr 25, 2002 9:34 pm
- Location: California USA
- Contact:
- puckeye
- Forum Contributor
- Posts: 105
- Joined: Fri Dec 06, 2002 7:26 pm
- Location: Joliette, QC, CA
- Contact:
COUNT() function revisited
HI,
Is there a quick way to count the total rows that would be returned when LIMIT is used.
I need to do something like XX out of XXX records.
Taking into account that there is more then 10 records meeting the X = Y condition how would I know how many actually met the condition without having to call a second query like
Thanks for help
Is there a quick way to count the total rows that would be returned when LIMIT is used.
I need to do something like XX out of XXX records.
Code: Select all
SELECT * FROM TABLE WHERE X = Y LIMIT 0, 10Code: Select all
SELECT * FROM TABLE WHERE X = Y- Sevengraff
- Forum Contributor
- Posts: 232
- Joined: Thu Apr 25, 2002 9:34 pm
- Location: California USA
- Contact:
i think you could just use mysql_affected_rows.
Code: Select all
$sql = "SELECT * FROM TABLE WHERE X = Y LIMIT 0, 10";
$runsql = mysql_query($sql);
$returned_rows=mysql_affected_rows();-
fractalvibes
- Forum Contributor
- Posts: 335
- Joined: Thu Sep 26, 2002 6:14 pm
- Location: Waco, Texas
-
fractalvibes
- Forum Contributor
- Posts: 335
- Joined: Thu Sep 26, 2002 6:14 pm
- Location: Waco, Texas