slj90 Posted January 15, 2010 Share Posted January 15, 2010 Hello, I am trying to display a random row from an sql table. The fields I want to display are... 'Username' and 'XP' I know the code to print data knowing the ID is.. <?php include ("connectionusers.php"); $ID = $_SESSION["authenticatedUser"] ; $ID = $_GET['ID'] ; $query = "SELECT * FROM users WHERE ID = $ID"; $result = mysql_query ($query); if ($row = mysql_fetch_array($result)) { print "<b>ID: </b>" . $row["ID"] . "<br>" ; print "<br><b>Username: </b>" . $row["Username"] . "<br>"; Could I use a code to generate a random number between 1 and 100, then place that number into 'ID' in the code above? Sorry if I haven't explained this very well and I appreciate any help. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/188539-print-a-random-row-from-mysql-table/ Share on other sites More sharing options...
calmchess Posted January 15, 2010 Share Posted January 15, 2010 I'd use array_push to push all my rows into an array and then use $ran=rand (0,100) to generate a random number then say $target = arr0[$ran]; Quote Link to comment https://forums.phpfreaks.com/topic/188539-print-a-random-row-from-mysql-table/#findComment-995353 Share on other sites More sharing options...
mapleleaf Posted January 15, 2010 Share Posted January 15, 2010 $id = rand(0,100); Quote Link to comment https://forums.phpfreaks.com/topic/188539-print-a-random-row-from-mysql-table/#findComment-995355 Share on other sites More sharing options...
tail Posted January 15, 2010 Share Posted January 15, 2010 mysql_query("SELECT `username`,`xp` FROM `users` WHERE `id`='".$id."' ORDER BY RAND() LIMIT 1"); I'm fairly certain that would be the fastest and most efficient way. Quote Link to comment https://forums.phpfreaks.com/topic/188539-print-a-random-row-from-mysql-table/#findComment-995358 Share on other sites More sharing options...
calmchess Posted January 15, 2010 Share Posted January 15, 2010 oh yeah i forgot that there is a random *sql command......use that ! Quote Link to comment https://forums.phpfreaks.com/topic/188539-print-a-random-row-from-mysql-table/#findComment-995360 Share on other sites More sharing options...
slj90 Posted January 15, 2010 Author Share Posted January 15, 2010 So it would be mysql_query("SELECT 'Username','XP' FROM 'users' WHERE 'ID'='".$id."' ORDER BY RAND() LIMIT 1") What do i need to put after to display the results? Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/188539-print-a-random-row-from-mysql-table/#findComment-995405 Share on other sites More sharing options...
tail Posted January 15, 2010 Share Posted January 15, 2010 $r=mysql_fetch_array(mysql_query("SELECT 'Username','XP' FROM 'users' WHERE 'ID'='".$id."' ORDER BY RAND() LIMIT 1")); echo 'Username: '.$$r['username'].' XP: '.$$r['xp']; Quote Link to comment https://forums.phpfreaks.com/topic/188539-print-a-random-row-from-mysql-table/#findComment-995515 Share on other sites More sharing options...
Buddski Posted January 15, 2010 Share Posted January 15, 2010 I am presuming that ID is unique to all users in the users table therefor if you are specifying the ID the RAND() will have no effect because it will be getting all the users with ID = $ID (which im guessing is 1) and then getting a random one from that set which is the only one there.. There are 2 approaches (assuming that I am right); $sql_query = mysql_query("SELECT `Username`,`XP` FROM `users` ORDER BY RAND() LIMIT 1"); or $sql_query = mysql_query("SELECT `Username`,`XP` FROM `users` WHERE `ID`='".rand(1,100)."'"); and FYI: echo 'Username: '.$$r['username'].' XP: '.$$r['xp']; will more than likely throw an undefined error as using that method will be looking for variables called, for example, $Someusername and $somexpthingo... Quote Link to comment https://forums.phpfreaks.com/topic/188539-print-a-random-row-from-mysql-table/#findComment-995658 Share on other sites More sharing options...
tail Posted January 15, 2010 Share Posted January 15, 2010 and FYI: echo 'Username: '.$$r['username'].' XP: '.$$r['xp']; will more than likely throw an undefined error as using that method will be looking for variables called, for example, $Someusername and $somexpthingo... If you didn't notice, I used the mysql_fetch_array() function, which puts the results into variable $r, as seen in my code. The only reason my code wouldn't work is because I put two $ by accident. Quote Link to comment https://forums.phpfreaks.com/topic/188539-print-a-random-row-from-mysql-table/#findComment-995833 Share on other sites More sharing options...
Buddski Posted January 16, 2010 Share Posted January 16, 2010 Im not saying that your code was entirely wrong but the double $ was a valid point, accident or not, not to mention the query itself may be wrong.. I was giving my 2 cents and pointing out some issues that may have been encountered.. I wasnt 'trying' to offend Quote Link to comment https://forums.phpfreaks.com/topic/188539-print-a-random-row-from-mysql-table/#findComment-995919 Share on other sites More sharing options...
tail Posted January 16, 2010 Share Posted January 16, 2010 Haha wow, the query is wrong. I'm never going to try coding anything at 7:00am again! Quote Link to comment https://forums.phpfreaks.com/topic/188539-print-a-random-row-from-mysql-table/#findComment-995986 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.