Jump to content

[SOLVED] Pulling Random rows from database - Simple


herghost

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.