Re: [PHP] determining number of rows in a mysql table
-{ Rene Brehmer }- wrote: 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 errorbr'.mysql_error()); if (mysql_num_rows($query) 0) { $result = mysql_fecth_array($query); // and so on ... } Use this code instead: if ($result = mysql_fetch_array($query)) { // do something } BTW, pls remember that an array of nothing (array()) is FALSE when it is casted into boolean. The comparison '!== FALSE' is not necessary just because mysql_fetch_array does not return empty arrays. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] determining number of rows in a mysql table
Hello Andy, Tuesday, March 30, 2004, 6:48:47 AM, you wrote: AB $rows=mysql_query(select count(*) as count from users); AB $count=mysql_fetch_array($rows); There's no need to extract the result into an array in this instance, why bother giving PHP that extra overhead? Unless the query failed, the value count will *always* exist at row 0, so you can simply do: $count = mysql_result($rows, 0, 'count'); -- Best regards, Richard Davey http://www.phpcommunity.org/wiki/296.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] determining number of rows in a mysql table
From: Richard Davey [EMAIL PROTECTED] AB $rows=mysql_query(select count(*) as count from users); AB $count=mysql_fetch_array($rows); There's no need to extract the result into an array in this instance, why bother giving PHP that extra overhead? Unless the query failed, the value count will *always* exist at row 0, so you can simply do: $count = mysql_result($rows, 0, 'count'); Or even just $count = mysql_result($rows,0); ;) ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] re:[PHP] determining number of rows in a mysql table
$count = mysql_result($rows, 0, 'count'); i guess that would work too if all you need is the # of rows in a table... if you need it in an array then... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] re:[PHP] determining number of rows in a mysql table
Hello Andy, Wednesday, March 31, 2004, 7:02:59 PM, you wrote: AB i guess that would work too if all you need is the # of rows in a table... AB if you need it in an array then... ... then you read the part of my post that said There's no need to extract the result into an array **in this instance**, why bother giving PHP that extra overhead? I **highlighted** the relevant part for you :) Don't forge that nearly all replies to this list are meant in the context of the original problem, not generically, and this was certainly no exception. -- Best regards, Richard Davey http://www.phpcommunity.org/wiki/296.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] re:[PHP] determining number of rows in a mysql table
I **highlighted** the relevant part for you :) i know what you mean... if you highlighted the part you were talking about with color then would explain why i didnt get what part you were talking about... my screen reader doesnt pay much attention to color highlights so probably missed it... sorry... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] re:[PHP] determining number of rows in a mysql table
Hello Andy, Wednesday, March 31, 2004, 7:21:44 PM, you wrote: AB i know what you mean... if you highlighted the part you were talking about AB with color then would explain why i didnt get what part you were talking AB about... my screen reader doesnt pay much attention to color highlights so AB probably missed it... I don't use colour in emails to this (or any) list, sorry. I would have hoped the screen reader said asterisk asterisk highlighted (etc). -- Best regards, Richard Davey http://www.phpcommunity.org/wiki/296.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] re:[PHP] determining number of rows in a mysql table
I don't use colour in emails to this (or any) list, sorry. I would have hoped the screen reader said asterisk asterisk highlighted all i heard it say was *** on the line that said something like ***highlighted... but thats whas only when you referrenced to you highlighted the stuff... anyways all is better now and i can drop out of this thread ... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] determining number of rows in a mysql table
select count(*) from tablename --- Chris Wagner [EMAIL PROTECTED] wrote: this should be easy... how would one go about determining the number of rows in a mysql table, without actually wasting the time of querying for *. is this the best way? $result = mysql_query('SELECT * FROM tablename'); $num_rows = mysql_num_rows($result); does this actually take up time searching? thanks. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php __ Do you Yahoo!? Yahoo! Finance Tax Center - File online. File on time. http://taxes.yahoo.com/filing.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] determining number of rows in a mysql table
Hello Chris, Tuesday, March 30, 2004, 3:12:26 AM, you wrote: CW how would one go about determining the number of rows in a mysql table, CW without actually wasting the time of querying for *. CW is this the best way? CW $result = mysql_query('SELECT * FROM tablename'); CW $num_rows = mysql_num_rows($result); SELECT COUNT(*) AS hits FROM tablename The row count will be in hits in $result. Count is a much faster way for MySQL to give you the number you want without actually selecting all the data in the process. -- Best regards, Richard Davey http://www.phpcommunity.org/wiki/296.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] determining number of rows in a mysql table
select count(*) from tablename Miles At 12:12 PM 3/30/2004 +1000, Chris Wagner wrote: this should be easy... how would one go about determining the number of rows in a mysql table, without actually wasting the time of querying for *. is this the best way? $result = mysql_query('SELECT * FROM tablename'); $num_rows = mysql_num_rows($result); does this actually take up time searching? thanks. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] determining number of rows in a mysql table
On Tuesday 30 March 2004 12:33, Andy B wrote: select count(*) from tablename actually i would do: $rows=mysql_query(select count(*) from tablename); echo $rows; Hmmm, have you ever actually tried it? And got the result you wanted? -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* The Microsoft Motto: We're the leaders, wait for us! */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] determining number of rows in a mysql table
On Tuesday 30 March 2004 13:48, Andy B wrote: 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']; ? That's better :-) -- Jason Wong - Gremlins Associates - www.gremlins.biz Open Source Software Systems Integrators * Web Design Hosting * Internet Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* You've been Berkeley'ed! */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] determining number of rows in a mysql table
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 errorbr'.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