RE: Mysql Rows

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



[snip]
my reasoning for needing the users number in a database is this...

i am going to be doing a lottery type thing where i grab a random number
between 1 and the result of mysql_num_rows($result)... that is the
reason
the gaps matter.  the while loop didn't work for me so if anyone could
help
me out on how to get this number i would aprreaciate it. thank you in
advance.
[/snip]

The one thing that no one has mentioned is that relational databases
will returns rows in whatever order they please if no ORDER BY is
specified. Generally, due to query caching, rows without an order by
will be returned in the same order nearly every time.

The better solution here is selecting the lowest ID and the highest ID
and selecting a random number between the two, checking for its
existence, and doing the operation again until it comes up with a valid
ID. This way it matters not what ID's exist in what order and you will
always get a valid ID to award the lottery to. Or you could do it all in
the query itself, if you wanted to be a true RDBMS master.

SELECT * FROM table ORDER BY RAND() LIMIT 1

See...no ID's or artificial ordering required.

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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux