CChard
09-02-2002, 08:32 PM
How do I get a random data entry from a row?
Like I have a table with ID, Name, URL.
I want to pick a random ID from just the ones in the table and echo its Namd and URL to the page.
The Prohacker
09-02-2002, 09:13 PM
<?php
$id = rand(0,4);
$sql_info = mysql_query("SELECT * FROM info WHERE id='$id'",$sql_con);
?>
This is very rough, and won't work without the rest of the info...
This assumes you have 5 rows in your db, just increase 4 to how many random things you have...
You might have to use another PHP function to get the number of rows in the table to set the max for the random num..
But this should give you a decent idea how todo it..
Noldar
09-02-2002, 09:21 PM
Here's how I've done it in the past.
$sql = "SELECT name, URL FROM some_table ORDER BY rand() LIMIT 1";
$result = mysql_query($sql, $connection);
$row = mysql_fetch_object($result);
echo "$row->name $row->URL";
Richard
jtrovato
09-02-2002, 11:03 PM
I like the last one.. that is very simple and clean.
CChard
09-03-2002, 03:26 PM
Thanks.
The first one won't work becuase of how often I might be changing the files.
But the second one should be great!
DimaDubin
09-03-2002, 04:35 PM
SELECT * FROM table ORDER BY RAND()
Rich2k
09-04-2002, 05:19 AM
Although all the later versions support RAND() I believe some of the version still floating around on some hosts don't support it.