Jump to content

[SOLVED] selecting a random members from database.


seany123

Recommended Posts

i have a database with all my members details in...

 

what im wanting to do is add a field called lottery... then when they enter... their lottery will = 1.

 

then i want some way of picking a random members from my database who has lottery = 1

 

then... making a query with the selected member.

I'm going to assume you know how to add a field called "lottery" and how to set it to '1' when a user chooses to enter the lottery.

 

For selecting random lottery winners...

 

$number_of_winners = 10; //change this to fit your needs
$winners = array();

for($i = 0; $i < $number_of_winners; $i++){
     $get_winner = mysql_query("SELECT * FROM `members` WHERE `lottery`='1' ORDER BY RAND() LIMIT 1");
     $winners[] = mysql_fetch_assoc($get_winner);
}

//$winners now contains a multi-dimensional array of 10 winners

 

Be sure to note that you may not actually have to query 10 winners separately, but possibly 10 winners in one query. I'm not sure exactly how ORDER BY RAND() works (whether it picks a random spot and then returns ordered rows from there or if it picks 10 rows independently and all at random). It probably randomizes each row selection. So, this may work...

 

$number_of_winners = 10;
$get_winners = mysql_query("SELECT * FROM `members` WHERE `lottery`='1' ORDER BY RAND() LIMIT ".$number_of_winners);

//use $get_winners at your discretion

 

But, again, I'm not sure and I'm too tired to test on my own. :)

I'm going to assume you know how to add a field called "lottery" and how to set it to '1' when a user chooses to enter the lottery.

 

For selecting random lottery winners...

 

$number_of_winners = 10; //change this to fit your needs
$winners = array();

for($i = 0; $i < $number_of_winners; $i++){
     $get_winner = mysql_query("SELECT * FROM `members` WHERE `lottery`='1' ORDER BY RAND() LIMIT 1");
     $winners[] = mysql_fetch_assoc($get_winner);
}

//$winners now contains a multi-dimensional array of 10 winners

 

Be sure to note that you may not actually have to query 10 winners separately, but possibly 10 winners in one query. I'm not sure exactly how ORDER BY RAND() works (whether it picks a random spot and then returns ordered rows from there or if it picks 10 rows independently and all at random). It probably randomizes each row selection. So, this may work...

 

$number_of_winners = 10;
$get_winners = mysql_query("SELECT * FROM `members` WHERE `lottery`='1' ORDER BY RAND() LIMIT ".$number_of_winners);

//use $get_winners at your discretion

 

But, again, I'm not sure and I'm too tired to test on my own. :)

 

yeah i think i understand what to do now...

 

i only want 1 winner so i just set it to 1 instead or remove it completely?

I tried to echo get winners and it didnt echo anything... hmm

 

Lol, well of course not. It's a result resource link. You have to use mysql_fetch_assoc() to get a usable array from the SELECT query.

 

$number_of_winners = 1;
$get_winners = mysql_query("SELECT * FROM `members` WHERE `lottery`='1' ORDER BY RAND() LIMIT ".$number_of_winners);

$winner = mysql_fetch_assoc($get_winners);

//$winner now stores an array of data for one particular (randomly chosen) member
//Assuming that members have a username...
echo $winner['username'];

I tried to echo get winners and it didnt echo anything... hmm

 

Lol, well of course not. It's a result resource link. You have to use mysql_fetch_assoc() to get a usable array from the SELECT query.

 

$number_of_winners = 1;
$get_winners = mysql_query("SELECT * FROM `members` WHERE `lottery`='1' ORDER BY RAND() LIMIT ".$number_of_winners);

$winner = mysql_fetch_assoc($get_winners);

//$winner now stores an array of data for one particular (randomly chosen) member
//Assuming that members have a username...
echo $winner['username'];

 

ah yes good point...

 

tried that and worked good :)

 

thanks

Archived

This topic is now archived and is closed to further replies.

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