...

View Full Version : Generate a random record



losse
04-30-2006, 02:51 PM
Hi

I have a table in a DB with various people's names in it... I was wondering if there is some script that I could use to randomly choose a name from that table...

This is the shell that will be holding this code:




echo "<table cellpadding=1 border=1 width=40%>";
echo "<tr>";
echo "<td>Random Name</td>";
echo "</tr>";
echo "<tr>";
echo "<td>$row[name] </td>";
echo "</tr>";
echo "</table>";



Right now the line in red above pulls out the name from the DB based on the $_GET['id'] but ideally I would like this to be a randomly chosen name from the DB...

Thanks for your help!

GJay
04-30-2006, 03:17 PM
SELECT * FROM table ORDER BY rand() LIMIT 1;

losse
04-30-2006, 03:22 PM
Thanks Gjay... so I alreay have this "SELECT" quiery in my script:



SELECT name FROM product WHERE id='$id'


But also on this page I want to generate the random name...

So would I add anothe SELECT quiery or modify this one?

Thanks

GJay
04-30-2006, 03:30 PM
If you want to display a random one as well as the specific one, you'd need a new query

losse
04-30-2006, 03:33 PM
Ok... So for the new quiery do I have to close out the PHP and then start a new one or can you have you "SELECT" quieries within one script?

Muhammad Haris
04-30-2006, 04:43 PM
echo rand($row[name]);

probably?

losse
04-30-2006, 11:54 PM
Thanks for your feedback... actually i had to create a whole new query... ended up doing this"




// create random query
$rand = "SELECT name FROM product ORDER BY RAND() LIMIT 2";

// execute query
$result = mysql_query($rand) or die ("Error in query: ".$rand."<br/>".mysql_error());
$row1 = mysql_fetch_array($result);
$row2 = mysql_fetch_array($result);

echo "<table cellpadding=1 border=1 width=100%>";
echo "<tr>";
echo "<td>Random Name:</td>";
echo "</tr>";
echo "<tr>";
echo "<td>$row1[name]</td>";
echo "</tr>";
echo "<tr>";
echo "<td>$row2[name]</td>";
echo "</tr>";
echo "</table>";


Found this in an example in a PHP for dummies book I have... It's just so much easier to reference the internet than a book... but sometimes... a little reading doesn't hurt

trib4lmaniac
05-01-2006, 03:02 PM
Thanks for your feedback... actually i had to create a whole new query... ended up doing this"




// create random query
$rand = "SELECT name FROM product ORDER BY RAND() LIMIT 2";

// execute query
$result = mysql_query($rand) or die ("Error in query: ".$rand."<br/>".mysql_error());
$row1 = mysql_fetch_array($result);
$row2 = mysql_fetch_array($result);

echo "<table cellpadding=1 border=1 width=100%>";
echo "<tr>";
echo "<td>Random Name:</td>";
echo "</tr>";
echo "<tr>";
echo "<td>$row1[name]</td>";
echo "</tr>";
echo "<tr>";
echo "<td>$row2[name]</td>";
echo "</tr>";
echo "</table>";


Found this in an example in a PHP for dummies book I have... It's just so much easier to reference the internet than a book... but sometimes... a little reading doesn't hurt
Oh, so I'm a PHP book for a dummy now am I? (http://www.sitepoint.com/forums/showthread.php?t=375287)

You don't think I stick to a single forum do you?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum