|
-
Mar 19th, 2009, 08:13 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Question on Mysql count.
Anyone know if there's a better approach than doing
Code:
$query = "SELECT COUNT(name) FROM members";
$result = mysql_query($query) or die(mysql_error());
simply want a count of the rows really and don't need anything returned.
oh oh oh ... is it possible to do a sort of "COUNT" "WHERE" thing to only count records matching a criteria?
-
Mar 19th, 2009, 09:08 PM
#2
Re: Question on Mysql count.
(1) The return value of that query is the row count, which is what you want...
(2) select count(*) from members where a=b
-
Mar 19th, 2009, 09:09 PM
#3
Re: Question on Mysql count.
you can use the mysql_num_rows() function to see how many rows resulted from your query, though you can use MySQL's COUNT() function to do add a WHERE clause, too. If you're only looking to return the number of rows (and not any actual data from the query), COUNT() might be faster.
PHP Code:
<?php $sql = mysql_query("SELECT id FROM table WHERE this='that' AND that='this'"); $num = mysql_num_rows($sql); echo "There were {$num} rows returned."; ?>
-
Mar 19th, 2009, 09:12 PM
#4
Thread Starter
Hyperactive Member
Re: Question on Mysql count.
Thanks dude that works just like a brought one
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
|