In article <180f01c1c403$2ac62820$[EMAIL PROTECTED]>,
[EMAIL PROTECTED] says...
> I have a little problem when trying to count the number of results returned
> in a mysql query. I was using mysql_num_rows() to do it with no problems,
> but this isn't as quick as using mySQL's COUNT(*), or so I have been told so
> I swtiched. Here's a snipit of the code...
>
> $sqlinfo = "SELECT username, COUNT(username) as count FROM usertable WHERE
> username='me' GROUP BY username";
> $sqlresult = mysql_query($sqlinfo)
> or die(mysql_error());
>
> $count = mysql_result($sqlresult,0,"count");
>
> if ($count <= 0) {
> ....FAILED....
> } else {
> while ($row = mysql_fetch_array($sqlresult)) {
> $username = $row['username'];
> }
> }
>
> The count value is set correctly but: when the while() loop is
> executed...no values are set (there are a lot more, but I shortened it for
> spaces sake). So, $username is null. If I remove the $count line, it
> works....any suggestions?
>
> Max
Assuming that your username is unique, I would expect that you would only
get one row returned from that query? In which case much of your SQL is
redundant.
Anyhow, your $count line reads the first row of the result, then sets the
pointer to the next row in the result set - if this is empty (ie only one
row retrieved) then you will get a null result for your while loop as
there are no more results to display. Try using mysql_data_seek to return
the pointer to row 0 before your while loop.
--
David Robley
Temporary Kiwi!
Quod subigo farinam
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php