|
-
Feb 28th, 2004, 08:49 AM
#1
Thread Starter
Frenzied Member
find number of records [resolved]
How do you find the number of records in a DB using MySQL?
Last edited by Acidic; Feb 29th, 2004 at 11:44 AM.
Have I helped you? Please Rate my posts. 
-
Feb 28th, 2004, 10:19 AM
#2
I'm assuming that as you posted this in the PHP forum you are using mysql and PHP. If that is the case then the:
mysql_num_rows() function will do the trick on a previously run select query:
PHP Code:
$result = mysql_query ("SELECT * FROM mytable;", $link);
echo ("Number of rows in table: " . mysql_num_rows($result));
-
Feb 28th, 2004, 12:48 PM
#3
You can also do a query to count it itself without calling a PHP function to get it.
PHP Code:
<?
$qry = mysql_query("SELECT COUNT(*) FROM table WHERE conditions='value'");
$arr = mysql_fetch_array($qry);
echo "number of records: $arr[0]\n";
?>
-
Feb 28th, 2004, 02:14 PM
#4
Which is much much faster.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 28th, 2004, 11:34 PM
#5
Stuck in the 80s
Originally posted by kows
You can also do a query to count it itself without calling a PHP function to get it.
PHP Code:
<?
$qry = mysql_query("SELECT COUNT(*) FROM table WHERE conditions='value'");
$arr = mysql_fetch_array($qry);
echo "number of records: $arr[0]\n";
?>
I know it's not what you meant, but mysql_query() and mysql_fetch_array() are both PHP functions.
-
Feb 28th, 2004, 11:35 PM
#6
Stuck in the 80s
Originally posted by CornedBee
Which is much much faster.
I'm not doubting you are calling you wrong, but where/how did you learn this?
-
Feb 28th, 2004, 11:54 PM
#7
I'm guessing that for PHP to count your query it's going to have to requery your query and add the MySQL COUNT() function to it, or it could loop through the query you give it and increment the count every loop through..
Using MySQL's internal COUNT() function just runs through the table once and counts every record.. and since it's an internal function it will run faster than PHP having to count the query.
Anyways, that's just my theory.
-
Feb 29th, 2004, 04:32 AM
#8
Originally posted by kows
I'm guessing that for PHP to count your query it's going to have to requery your query and add the MySQL COUNT() function to it, or it could loop through the query you give it and increment the count every loop through..
Using MySQL's internal COUNT() function just runs through the table once and counts every record.. and since it's an internal function it will run faster than PHP having to count the query.
Anyways, that's just my theory.
mysql always returns the number of records in a query. PHP need just query that.
As for which method is quicker - I'm not sure.
With my method mysql has to create a table containing all the records and fields and store it in memory.
In your method only the only record returned is one which contains the number of records in the table. So I guess that would mean that it is quicker.
-
Feb 29th, 2004, 01:27 PM
#9
Spot on visualAd. The lack of actual records returned makes for much space and speed savings. ESPECIALLY if the database is on a different server than the script (uncommon).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 29th, 2004, 01:42 PM
#10
Ex-Super Mod'rater
Well it would be best to use the PHP count function if your going to be needing all the records anyway. However if your only interested in how many it'd be better to use MySQL count(primary key). That probably went with out saying but I thought I'd stop the lot of you just deciding to stick with one of these for all situations .
When your thread has been resolved please edit the original post in the thread (  )
and amend "-[RESOLVED]-" to the end of the title and change the icon to  , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

-
Feb 29th, 2004, 02:03 PM
#11
Thread Starter
Frenzied Member
OK, thanks guys.
I think I'm still too new to PHP/MySQL to comprehend what is being said. For me it works, I know I shouldn't think like that, but I do.
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|