Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/188539-print-a-random-row-from-mysql-table/
Share on other sites

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...

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.

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 :P

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.