ben1989 Posted June 20, 2006 Share Posted June 20, 2006 hi i have been registerd b4 but i forget all mi login stuff lolim building a php site but on the home page i would like it to pull rando info from a table the table is labed members and the rows are ID, membername, location, country, age, genderi would like it to randoly pull the id and display the info for that id if u know what i mean thnaks-benneed for info just ask :) Quote Link to comment https://forums.phpfreaks.com/topic/12469-random-display/ Share on other sites More sharing options...
Ferenc Posted June 20, 2006 Share Posted June 20, 2006 Something like this would work, as long as the id's are numerical, and don't have gaps in them (1,2,5,6 will error periodicly).[code]<?php$sql = "SELECT * FROM members";$result = mysql_query($sql)or die (mysql_error());// get the total$total = mysql_num_rows($result) or die (mysql_error());// get a random id from the data base..$rand = rand(1,$total);$sql .= " WHERE ID = '$rand'";$rand_sql = mysql_query($sql) or die (mysql_error());while ($output = mysql_fetch_array($rand_sql)){ echo $output['membername']."<br>"; echo $output['location']; }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12469-random-display/#findComment-47704 Share on other sites More sharing options...
ben1989 Posted June 20, 2006 Author Share Posted June 20, 2006 [!--quoteo(post=386057:date=Jun 20 2006, 10:40 AM:name=Ferenc)--][div class=\'quotetop\']QUOTE(Ferenc @ Jun 20 2006, 10:40 AM) [snapback]386057[/snapback][/div][div class=\'quotemain\'][!--quotec--]Something like this would work, as long as the id's are numerical, and don't have gaps in them (1,2,5,6 will error periodicly).[code]<?php$sql = "SELECT * FROM members";$result = mysql_query($sql)or die (mysql_error());// get the total$total = mysql_num_rows($result) or die (mysql_error());// get a random id from the data base..$rand = rand(1,$total);$sql .= " WHERE q_id = '$rand'";$rand_sql = mysql_query($sql) or die (mysql_error());while ($output = mysql_fetch_array($rand_sql)){ echo $output['membername']."<br>"; echo $output['location']; }?>[/code][/quote]hah ill try that thanks m8exelent thnaks you just one last thing how can i do it so it dose it 5 times??selects the random id 5 times and echos all 5 results?thanks -ben Quote Link to comment https://forums.phpfreaks.com/topic/12469-random-display/#findComment-47706 Share on other sites More sharing options...
Ferenc Posted June 20, 2006 Share Posted June 20, 2006 [!--quoteo(post=386059:date=Jun 20 2006, 09:46 AM:name=ben_stringer)--][div class=\'quotetop\']QUOTE(ben_stringer @ Jun 20 2006, 09:46 AM) [snapback]386059[/snapback][/div][div class=\'quotemain\'][!--quotec--]hah ill try that thanks m8exelent thnaks you just one last thing how can i do it so it dose it 5 times??selects the random id 5 times and echos all 5 results?thanks -ben[/quote]place it in a for loop for($i = 0; $i < 5 ;$i++){ //code to display 5x} Quote Link to comment https://forums.phpfreaks.com/topic/12469-random-display/#findComment-47713 Share on other sites More sharing options...
ben1989 Posted June 20, 2006 Author Share Posted June 20, 2006 hmm im havin a alittle truble were do i pu it coz i have just tryed and i get the same 5 times lol i am a bit off a newbi sorry Quote Link to comment https://forums.phpfreaks.com/topic/12469-random-display/#findComment-47716 Share on other sites More sharing options...
Ferenc Posted June 20, 2006 Share Posted June 20, 2006 $sql = "SELECT * FROM members";$result = mysql_query($sql)or die (mysql_error());// get the total$total = mysql_num_rows($result) or die (mysql_error());// get a random id from the data base..// begin for loopfor($i = 0; $i < 5 ;$i++){ $rand = rand(1,$total); $sql .= " WHERE ID = '$rand'"; $rand_sql = mysql_query($sql) or die (mysql_error()); while ($output = mysql_fetch_array($rand_sql)){ echo $output['membername']."<br>"; echo $output['location']; }// end for loop} Quote Link to comment https://forums.phpfreaks.com/topic/12469-random-display/#findComment-47721 Share on other sites More sharing options...
ben1989 Posted June 20, 2006 Author Share Posted June 20, 2006 im gettin 1 result lol and an error You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ID = '1'' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/12469-random-display/#findComment-47722 Share on other sites More sharing options...
.josh Posted June 20, 2006 Share Posted June 20, 2006 ditch all that. you don't need php to do that for you when you can have mysql do it for you. [code]$sql = "SELECT * FROM members ORDER BY RAND() limit 5";$result = mysql_query($sql);while ($list = mysql_fetch_array($result)) { foreach ($list as $key => $val) { echo $key . " : " . $val . " "; } echo "<br>";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12469-random-display/#findComment-47724 Share on other sites More sharing options...
Ferenc Posted June 20, 2006 Share Posted June 20, 2006 Yes, that is much easier... Quote Link to comment https://forums.phpfreaks.com/topic/12469-random-display/#findComment-47725 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.