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

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?

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>

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>";


}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.