herghost Posted April 19, 2009 Share Posted April 19, 2009 Hi All, I have this which pulls a random band name from a database: <?php $sql = "SELECT * FROM user ORDER BY RAND()"; $result = mysql_query($sql ,$con); $myrow = mysql_fetch_array($result); echo $myrow['bandname']; ?> How would I Display more than one record? Thanks Link to comment https://forums.phpfreaks.com/topic/154799-solved-pulling-random-rows-from-database-simple/ Share on other sites More sharing options...
alphanumetrix Posted April 19, 2009 Share Posted April 19, 2009 Should just have to change your limit: <?php $limit = 50; $sql = "SELECT * FROM user ORDER BY RAND() LIMIT $limit"; $result = mysql_query($sql ,$con); $myrow = mysql_fetch_array($result); echo $myrow['bandname']; ?> Link to comment https://forums.phpfreaks.com/topic/154799-solved-pulling-random-rows-from-database-simple/#findComment-814098 Share on other sites More sharing options...
jackpf Posted April 19, 2009 Share Posted April 19, 2009 You need to put your mysql_fetch_array() in a while() loop. Link to comment https://forums.phpfreaks.com/topic/154799-solved-pulling-random-rows-from-database-simple/#findComment-814105 Share on other sites More sharing options...
herghost Posted April 19, 2009 Author Share Posted April 19, 2009 Thanks to you both, I know have this: <?php $limit = 2; $sql = "SELECT * FROM user ORDER BY RAND() LIMIT $limit"; $result = mysql_query($sql ,$con); $myrow = mysql_fetch_array($result); echo "<table width ='90%'>"; echo "<tr>"; echo "<td>"; echo $myrow['bandname']; echo "</td>"; echo "<td>"; echo "<a href='newprofile.php?userid="; echo $myrow['userid']; echo "'>View</a>"; echo "</td>"; echo "<tr>"; echo "<td>"; echo $myrow['bandname']; echo "</td>"; echo "<td>"; echo "<a href='newprofile.php?userid="; echo $myrow['userid']; echo "'>View</a>"; echo "</td>"; echo "</table>"; ?></p> However the same result is repeated. How would I put this in a while loop? Link to comment https://forums.phpfreaks.com/topic/154799-solved-pulling-random-rows-from-database-simple/#findComment-814108 Share on other sites More sharing options...
jackpf Posted April 19, 2009 Share Posted April 19, 2009 while($fetch = mysql_fetch_array($sql)) { echo $fetch['something']; } Link to comment https://forums.phpfreaks.com/topic/154799-solved-pulling-random-rows-from-database-simple/#findComment-814112 Share on other sites More sharing options...
herghost Posted April 19, 2009 Author Share Posted April 19, 2009 Thanks, Still dont think I am understanding this, I am getting this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in Link to comment https://forums.phpfreaks.com/topic/154799-solved-pulling-random-rows-from-database-simple/#findComment-814114 Share on other sites More sharing options...
jackpf Posted April 19, 2009 Share Posted April 19, 2009 You need to replace $sql with the variable that contains your mysql_query(). Link to comment https://forums.phpfreaks.com/topic/154799-solved-pulling-random-rows-from-database-simple/#findComment-814115 Share on other sites More sharing options...
herghost Posted April 19, 2009 Author Share Posted April 19, 2009 it is $sql ??? Why wont the bloody thing do what I want it to as opposed to what I tell it to do ??? Link to comment https://forums.phpfreaks.com/topic/154799-solved-pulling-random-rows-from-database-simple/#findComment-814117 Share on other sites More sharing options...
jackpf Posted April 19, 2009 Share Posted April 19, 2009 It looks like $result to me. Link to comment https://forums.phpfreaks.com/topic/154799-solved-pulling-random-rows-from-database-simple/#findComment-814118 Share on other sites More sharing options...
herghost Posted April 19, 2009 Author Share Posted April 19, 2009 just spotted that No error but now it doesn't actually display the results <?php $sql = "SELECT * FROM user ORDER BY RAND() LIMIT 5"; $result = mysql_query($sql ,$con); while($myrow = mysql_fetch_array($result)); { //$myrow = mysql_fetch_array($result); //while (mysql_num_rows($result) > 0); echo "<table width ='90%'>"; echo "<tr>"; echo "<td>"; echo $myrow['bandname']; echo "</td>"; echo "<td>"; echo "<a href='newprofile.php?userid="; echo $myrow['userid']; echo "'>View</a>"; echo "</td>"; echo "<tr>"; echo "<td>"; echo $myrow['bandname']; echo "</td>"; echo "<td>"; echo "<a href='newprofile.php?userid="; echo $myrow['userid']; echo "'>View</a>"; echo "</td>"; echo "</table>"; } ?></p> Link to comment https://forums.phpfreaks.com/topic/154799-solved-pulling-random-rows-from-database-simple/#findComment-814119 Share on other sites More sharing options...
jackpf Posted April 19, 2009 Share Posted April 19, 2009 You shouldn't have a semicolon after your while() statement, and also, put or die(mysql_error()); after your query. Link to comment https://forums.phpfreaks.com/topic/154799-solved-pulling-random-rows-from-database-simple/#findComment-814120 Share on other sites More sharing options...
herghost Posted April 19, 2009 Author Share Posted April 19, 2009 Thanks, This did it: <?php $sql = "SELECT * FROM user ORDER BY RAND() LIMIT 5"; $result = mysql_query($sql ,$con); while($myrow = mysql_fetch_array($result)) { //$myrow = mysql_fetch_array($result); //while (mysql_num_rows($result) > 0); echo "<table width ='90%'>"; echo "<tr>"; echo "<td>"; echo $myrow['bandname']; echo "</td>"; echo "<td>"; echo "<a href='newprofile.php?userid="; echo $myrow['userid']; echo "'>View</a>"; echo "</td>"; } ?> Link to comment https://forums.phpfreaks.com/topic/154799-solved-pulling-random-rows-from-database-simple/#findComment-814125 Share on other sites More sharing options...
jackpf Posted April 19, 2009 Share Posted April 19, 2009 Jolly good. Link to comment https://forums.phpfreaks.com/topic/154799-solved-pulling-random-rows-from-database-simple/#findComment-814126 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.