morocco-iceberg Posted May 9, 2010 Share Posted May 9, 2010 I'm trying to get an array that I've populated using a database to shuffle so that I can use it to ouput a random question. Here's my code: <?php mysql_connect("server", "user", "password") or die(mysql_error()); echo "Connected to MySQL<br/>"; mysql_select_db("database") or die(mysql_error()); echo "Connected to Database"; $result = mysql_query('SELECT * FROM table') or exit(mysql_error()); while ($row = mysql_fetch_assoc($result)){ $array[] = $row; } shuffle($array); print_r($array); ?> This doesn't seem to shuffle it at all... Any help is greatly appreciated. Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted May 9, 2010 Share Posted May 9, 2010 Why not let MySQL do it for you SELECT * FROM table ORDER BY RAND() Quote Link to comment Share on other sites More sharing options...
niranjan81 Posted May 9, 2010 Share Posted May 9, 2010 If its a fixed set of questions that re modified rarely, why bother database at all, built one yourself. that will save a round to database. Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted May 9, 2010 Share Posted May 9, 2010 What exactly does the print_r show? the code you have should work as it is Quote Link to comment Share on other sites More sharing options...
morocco-iceberg Posted May 10, 2010 Author Share Posted May 10, 2010 I was using the print_r to show the entire array. This is what I get when I print it out... I think the problem happens when I'm tryinto put it into an array, but I can't find any solutions. Array ( [0] => Array ( [Question 1.] => [Question 2.] => [Question 3.] => [Question 4.] => [Question 5.] => ) ) Quote Link to comment Share on other sites More sharing options...
morocco-iceberg Posted May 10, 2010 Author Share Posted May 10, 2010 Don't worry... my database was all screwed up. Thanks for the help though. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.