redirect to random URL

XK8ER
XK8ER used Ask the Experts™
on
hello there..
I have a forum and I would like to send users to a link where it will redirect them to a random thread..
im doing the mysql query like this

SELECT threadid FROM thread WHERE forumid=2 ORDER BY threadid DESC LIMIT 10

so I need help getting this thing together so that I can send them to the site like this

header("Location: http://warezun.com/showthread.php?t=".$URL);
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Try using ORDER BY RAND() instead of ORDER BY threadiid.

Then, just grab the first row in the result, and use that value:

$rs = mysql_query("SELECT threadID FROM thread WHERE forumid=2 ORDER BY RAND() LIMIT 1");
$row = mysql_fetch_assoc($rs);
header("Location: http://warezun.com/showthread.php?t=".$row["threadid"]);

Author

Commented:
the issue is that I have over 5k posts so im trying to show a random one with the last 10
Try this:
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
 
$max = 10;
$result = mysql_query("SELECT threadid FROM thread WHERE forumid=2 ORDER BY threadid DESC LIMIT " . $max);
$max = mysql_num_rows($result);
$i = rand(0, $max - 1);
 
if (mysql_data_seek($result, $i) && ($row = mysql_fetch_array($result, MYSQL_NUM))) {
    header("Location: http://warezun.com/showthread.php?t=".$row[0]);
}
 
mysql_free_result($result);
?>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial