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