Jump to content

Pulling random data.


skatermike21988

Recommended Posts

I am working on a project of creating a community script, i am wanting to pull 2-3 random new or old people from my database useing mysql, kinda like myspace's "cool new people" and i am having a hard time getting it to work this is my code:

[code]
//Pull and Display cool peorple
$sql = mysql_query("SELECT display_name FROM users LIMIT 2") or die (mysql_error());
while($row = mysql_fetch_array($sql)){
    $row_array[] = $row['display_name'];
}
$random_row = $row_array[rand(1, count($row_array) - 1)];

$data = mysql_query("SELECT * FROM users WHERE display_name='$random_row'")
or die(mysql_error());
while($info = mysql_fetch_array( $data ))
{
Print "<a href='http://www.profiles.friendshideout.com/?friendid=".$info['user_id'] . " ";
Print "&sid=$sid' target='_self'>";

Print "<img src='".$info['default_photo'] . "' height='75' width='75'>";
echo "<br>$random_row</a></td>";

}
echo "</tr></table>";
[/code]

When useing that code it is only pulling one person out and it is always the same.

Any help is appreciated.
Link to comment
https://forums.phpfreaks.com/topic/19701-pulling-random-data/
Share on other sites

Looks like far too much code. For a relatively small database, change your query to do the random selection for you:

[code]//Pull and Display cool random people
$sql = mysql_query("SELECT display_name FROM users ORDER by rand() LIMIT 2") or die (mysql_error());
... plus some more
[/code]
Link to comment
https://forums.phpfreaks.com/topic/19701-pulling-random-data/#findComment-85963
Share on other sites

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.