unidox Posted August 2, 2008 Share Posted August 2, 2008 I am using this code: <?php $q = mysql_query("SELECT * FROM `table`"); $r = mysq_fetch_array($q); $r = array_rand($r); echo $r['column']; ?> But that doesnt work. What I am trying to do, is select a random row from the db, then I can display the columns. How would I do that? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/117798-solved-rand/ Share on other sites More sharing options...
cooldude832 Posted August 2, 2008 Share Posted August 2, 2008 You need to realize what mysql_fetch_array is doing mysql_fetch_array produces the "next" row in the query resource to be displayed. using array_rand($row) will only grab a random field from that query row. http://www.phpfreaks.com/forums/index.php/topic,125759.0.html that shows exactly how to do a proper query for a random row. Quote Link to comment https://forums.phpfreaks.com/topic/117798-solved-rand/#findComment-605873 Share on other sites More sharing options...
.josh Posted August 2, 2008 Share Posted August 2, 2008 $q = mysql_query(" SELECT * FROM table ORDER BY rand( ) LIMIT 1 "); $r = mysql_fetch_array($q); foreach ($r as $key => $val) { echo "$key => $val <br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/117798-solved-rand/#findComment-605875 Share on other sites More sharing options...
cooldude832 Posted August 2, 2008 Share Posted August 2, 2008 now how are they suppose to learn if you spoon feed them the script they need (poorly written I might add also) The point is to teach the person how to fish not give them a fish. Quote Link to comment https://forums.phpfreaks.com/topic/117798-solved-rand/#findComment-605876 Share on other sites More sharing options...
.josh Posted August 2, 2008 Share Posted August 2, 2008 now how are they suppose to learn if you spoon feed them the script they need (poorly written I might add also) The point is to teach the person how to fish not give them a fish. You obviously don't have very much experience teaching people. Quote Link to comment https://forums.phpfreaks.com/topic/117798-solved-rand/#findComment-605882 Share on other sites More sharing options...
JasonLewis Posted August 2, 2008 Share Posted August 2, 2008 People learn differently. Some find it easier to learn by looking at what they did, comparing it to the answer then looking at the differences to see where they went wrong. Quote Link to comment https://forums.phpfreaks.com/topic/117798-solved-rand/#findComment-605884 Share on other sites More sharing options...
cooldude832 Posted August 2, 2008 Share Posted August 2, 2008 obviously more than you have since I don't just give them a code snippet without an explanation of why their methods are invalid or inappropriate for their goals and what your method does. You really shouldn't dabble in the php boards if you aren't willing to give complete and proper answers to people. Quote Link to comment https://forums.phpfreaks.com/topic/117798-solved-rand/#findComment-605885 Share on other sites More sharing options...
MasterACE14 Posted August 2, 2008 Share Posted August 2, 2008 @unidox. You have a minor typo. $r = mysq_fetch_array($q); missing the l after mysq $r = mysql_fetch_array($q); Quote Link to comment https://forums.phpfreaks.com/topic/117798-solved-rand/#findComment-605901 Share on other sites More sharing options...
GingerRobot Posted August 2, 2008 Share Posted August 2, 2008 To be fair, I always thought being able to write something comprehensible was rather a prerequisite for being able to teach someone... Quote Link to comment https://forums.phpfreaks.com/topic/117798-solved-rand/#findComment-605908 Share on other sites More sharing options...
unkwntech Posted August 2, 2008 Share Posted August 2, 2008 I love how the PHP forum has suddenly turned into a Argument forum... @unidox did you get an answer your question? Quote Link to comment https://forums.phpfreaks.com/topic/117798-solved-rand/#findComment-605957 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.