skatermike21988 Posted September 4, 2006 Share Posted September 4, 2006 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 More sharing options...
AndyB Posted September 4, 2006 Share Posted September 4, 2006 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 More sharing options...
extrovertive Posted September 4, 2006 Share Posted September 4, 2006 $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. Link to comment https://forums.phpfreaks.com/topic/19701-pulling-random-data/#findComment-85965 Share on other sites More sharing options...
skatermike21988 Posted September 5, 2006 Author Share Posted September 5, 2006 thanks worked pefect :) Link to comment https://forums.phpfreaks.com/topic/19701-pulling-random-data/#findComment-86169 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.