hi echoing a radom sql value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

hi echoing a radom sql value

Post by reecec »

Hi if i have this radom sql code

how would i get it to give me the values so i could echo the radom value



SELECT column FROM table
ORDER BY RAND()
LIMIT 1


thanks reece
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: hi echoing a radom sql value

Post by RobertGonzalez »

Code: Select all

<?php
$sql = "SELECT column 
        FROM table 
        ORDER BY RAND() 
        LIMIT 1";

if (!$result = mysql_query($sql))
{
    die("There was an error in the query $sql: " . mysql_error());
}

while ($row = mysql_fetch_array($result))
{
    echo $row['column'] . ' is the random string returned from the table...';
}
?>
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

useing mysql_fetch_array or object to get just 1 field isnt necessary!

Code: Select all

$query ="SELECT column
        FROM table
        ORDER BY RAND()
        LIMIT 1"; 
$result = mysql_query($query) or die("error");
$field = mysql_result($result,0);
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

mysql_result() is PHP's slowest ever function. It's good practise to never use it because it's really so bad.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Flamie wrote:useing mysql_fetch_array or object to get just 1 field isnt necessary!
I have used the exact same code I posted above with absolutely no performance issues at (mysql_fetch_array). Yes, it is only one result, but it is still fast and manages to do what is needed without a lot of overhead (one loop through an array of one values). It is also consisten with a lot of other code in the scripts I use so it maintains the flow of development I follow. Not to mention, it is a straight forward, easy to understand snippet without any performance issued like mysql_result().

PS Thanks onion for the bit about mysql_result(). That is a handy piece of info to have. Even the manual page mentions that it is a slow function in some instances.
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

=O I didnt know it was slow :x hehe, now I know at least (=
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

thanks all


sorry can some one tell me why my (rand 1, 2)

always comes out as the first number i put


thanks reece
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

hi

Post by reecec »

sorry rand (1,2)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Have you looked at the MySQL manual for the RAND() function and its uses?
Post Reply