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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$count_q = mysql_query("SELECT COUNT(lottery) FROM `members` WHERE `lottery`='1'");
$count_assoc = mysql_fetch_assoc($count_q);

$amount_of_members_with_lottery_equal_equal_1 = $count_assoc['COUNT(lottery)'];

Link to comment
Share on other sites

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.