chocopi Posted May 26, 2007 Share Posted May 26, 2007 How would i go about selecting information from a table and then picking a random one out of the selected <?php $1 = mysql_query("SELECT id FROM Mark WHERE id > 0"); rand($1); ?> or something like that Thanks, ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/53059-solved-sql-select-random/ Share on other sites More sharing options...
taith Posted May 26, 2007 Share Posted May 26, 2007 not tested... but it should work :-) <?php $query=mysql_query("SELECT id FROM Mark WHERE id>0 ORDER BY rand(), LIMIT 1"); $row=mysql_fetch_assoc(); print_r($row); ?> Quote Link to comment https://forums.phpfreaks.com/topic/53059-solved-sql-select-random/#findComment-262117 Share on other sites More sharing options...
egorig Posted May 26, 2007 Share Posted May 26, 2007 Another one ... <? $query = mysql_query ("SELECT id,name FROM table WHERE id>0"); $rand = rand(0,10); $result = mysql_result($query,$rand,"name"); echo $result; // this will display a random name from the table with names. ?> Quote Link to comment https://forums.phpfreaks.com/topic/53059-solved-sql-select-random/#findComment-262118 Share on other sites More sharing options...
chocopi Posted May 26, 2007 Author Share Posted May 26, 2007 cheers taith but it wasnt working so i put mysql_error in there and it returned: 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 'LIMIT 1' at line 1 egorig, thanks i shall try yours in a mo ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/53059-solved-sql-select-random/#findComment-262121 Share on other sites More sharing options...
cmgmyr Posted May 26, 2007 Share Posted May 26, 2007 <?php $result = mysql_query("SELECT id FROM Mark WHERE id > 0 ORDER BY RAND() LIMIT 1"); ?> This should work like taith said. It's always faster if you can just do it in the query itself Quote Link to comment https://forums.phpfreaks.com/topic/53059-solved-sql-select-random/#findComment-262124 Share on other sites More sharing options...
taith Posted May 26, 2007 Share Posted May 26, 2007 lol... right... no , after rand()... lol Quote Link to comment https://forums.phpfreaks.com/topic/53059-solved-sql-select-random/#findComment-262128 Share on other sites More sharing options...
chocopi Posted May 26, 2007 Author Share Posted May 26, 2007 thanks for all your help guys, but this is what i ended up with: <?php $query = mysql_query("SELECT id FROM table WHERE id>0 ORDER BY rand() LIMIT 1"); $row = mysql_fetch_assoc($query) or die(mysql_error()); $blah = $row['id']; echo $blah; ?> However it is only returning 1 and none of the other values ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/53059-solved-sql-select-random/#findComment-262133 Share on other sites More sharing options...
taith Posted May 26, 2007 Share Posted May 26, 2007 a) your only selecting the id b) your only echoing the id c) you've limited it to one row... you'll only ever get, one row... Quote Link to comment https://forums.phpfreaks.com/topic/53059-solved-sql-select-random/#findComment-262135 Share on other sites More sharing options...
chocopi Posted May 26, 2007 Author Share Posted May 26, 2007 ive changed echo $blah; to print ("Hello $blah "); and its working fine, strange Anyways, thanks for your help ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/53059-solved-sql-select-random/#findComment-262136 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.