Jump to content

Archived

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

skatermike21988

Pulling random data.

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.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
$random_row = $row_array[rand(1, count($row_array) - 1)];

Change that to:

$random_row = $row_array[rand([b]0[/b], count($row_array) - 1)];

Remember, array index starts at 0.

Share this post


Link to post
Share on other sites

×

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.