simonp Posted November 23, 2007 Share Posted November 23, 2007 Hello, I'm using this code to display the first 8 results from a query but would like to be able to show 8 random results instead - is this possible? Cheers Simon <?php //get stuff from db // Make the connection $db = mysql_connect($myHost, $myUser, $myPass); // Select the database mysql_select_db($myDB,$db); $sql = "SELECT * from $myTable"; // Get the query object @$result=mysql_query($sql,$db); // How many rows? $numRows = mysql_num_rows($result); // Initialise output $output = ''; //for($i=0; $i<$numRows; $i++){ for($i=0; $i<8; $i++){ $row = mysql_fetch_array($result); $output .= '<TD>user=' . $row['user'] . 'victim=' . $row['victim'] . '</TD>'; etc } // Close the database connection mysql_close($db); Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 23, 2007 Share Posted November 23, 2007 You can do it all with the mysql query, and use a foreach loop: <?php //get stuff from db // Make the connection $db = mysql_connect($myHost, $myUser, $myPass); // Select the database mysql_select_db($myDB,$db); $sql = "SELECT * from $myTable ORDER BY RAND() LIMIT 8"; // Get the query object @$result=mysql_query($sql,$db); // How many rows? $numRows = mysql_num_rows($result); // Initialise output $output = ''; foreach($row as mysql_fetch_assoc($result)){ $output .= '<TD>user=' . $row['user'] . 'victim=' . $row['victim'] . '</TD>'; etc } // Close the database connection mysql_close($db); Quote Link to comment Share on other sites More sharing options...
simonp Posted November 23, 2007 Author Share Posted November 23, 2007 Hi again, Thanks for that - I keep getting an error though and can't see what I've missed: Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' on the foreach line . . . I've tried moving things about but can't make it work - any suggestions!? Cheers Simon Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 23, 2007 Share Posted November 23, 2007 Corr, i'd love to know what i was thinking when i posted that code. It needs to be a while loop: <?php //get stuff from db // Make the connection $db = mysql_connect($myHost, $myUser, $myPass); // Select the database mysql_select_db($myDB,$db); $sql = "SELECT * from $myTable ORDER BY RAND() LIMIT 8"; // Get the query object @$result=mysql_query($sql,$db); // How many rows? $numRows = mysql_num_rows($result); // Initialise output $output = ''; while($row = mysql_fetch_assoc($result)){ $output .= '<TD>user=' . $row['user'] . 'victim=' . $row['victim'] . '</TD>'; etc } // Close the database connection mysql_close($db); In my defence, i do have a cold Quote Link to comment Share on other sites More sharing options...
simonp Posted November 23, 2007 Author Share Posted November 23, 2007 Once again GingerRobot - brilliant! Thanks for your help today. Get well soon! Cheers Simon 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.