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 Quote Link to comment 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']; ?> Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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']; } Quote Link to comment 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 Quote Link to comment 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(). Quote Link to comment 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 ??? Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 19, 2009 Share Posted April 19, 2009 It looks like $result to me. Quote Link to comment 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> Quote Link to comment 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. Quote Link to comment 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
jackpf Posted April 19, 2009 Share Posted April 19, 2009 Jolly good. 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.