Justtechjobs.com Find a programming school near you






Online Campus Both


php-general | 2004032

Re: [PHP] determining number of rows in a mysql table From: -{ Rene Brehmer }- (metalbunny <email protected>)
Date: 03/30/04

Thought it looked fishy .... hehe ...

My only problem is: Sometimes you actually need the data from that table
later in the same script, so instead of doing another data pull, I have
alot of cases where this is alot more useful (not actual code from any of
my work, but I've used this structure in the past):

$query = mysql_query("SELECT $fields FROM $table WHERE $crit") or
die('fetch error<br>'.mysql_error());

if (mysql_num_rows($query) > 0) {
   $result = mysql_fecth_array($query);
   // and so on ...
}

combined, this is actually faster than doing the COUNT(*) first, and
another query later IF (and only IF) you need the data as is ... but if you
need the number of rows to figure out how many pages you need to display
all of the data, then counting first, and then doing a data pull with the
limits on is faster ...

No rules without exceptions :)

Rene

At 06:48 30-03-2004, Andy B wrote:
>"Hmmm, have you ever actually tried it? And got the result you wanted?"
>
>my screwup...forgot some code here... heres the whole thing that actually
>works...(thats what you get for being in tons of different windows at the
>same time)...
><?
>include("conf.db");
>mysql_connect($host, $mysqluser, $mysqlpwd);
>$rows=mysql_query("select count(*) as count from users");
>$count=mysql_fetch_array($rows);
>
>echo $count['count'];
>?>

-- 
Rene Brehmer
aka Metalbunny

~ If you don't like what I have to say ... don't read it ~

http://metalbunny.net/ References, tools, and other useful stuff...

-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php