0
$query = mysql_query("SELECT Name from lottery");
// Make All Lotteries Into An Array
$queryarray = mysql_fetch_array($query);
// For Each Lottery, Store the Name of It in $lottery
foreach ($queryarray as $lottery) {
    // Select IDs of Tickets in Current Selected Lottery ($lottery)
    $ticketquery = mysql_query("SELECT id FROM tickets WHERE lottery='$lottery'");
    // Create an Array from the IDS
    $ticketarray = mysql_fetch_assoc($ticketquery);
    // Select A Random ID, This Is Our Winner Ticket
    $winner = $ticketarray[array_rand($ticketarray)];
}

When I run this, however, I get this error:


"Warning: array_rand() [function.array-rand]: First argument has to be an array in public_html/php/lotterypick.php on line 25"
I have tried to replace mysql_fetch_assoc with mysql_fetch_array and that hasn't helped much either. Just given me the same error. Any ideas? Thanks.

Line 25: $winner = $ticketarray[array_rand($ticketarray)];

12
  • 2
    You really should learn about SQL joins... Commented Mar 28, 2015 at 19:30
  • Put var_dump($queryarray);die; before the loop, and see what it contains... it does not contain what you think it contains. Commented Mar 28, 2015 at 19:30
  • Oh wow. Yeah, that is definitely not what I thought. How would I fix that? @SverriM.Olsen Commented Mar 28, 2015 at 19:33
  • But I don't understand how combing rows from the tables would solve this issue? Or would it? @eggyal Commented Mar 28, 2015 at 19:36
  • 1
    @TonyStark: Or you could just ask MySQL to select a random ticket from each lottery, as one query. Commented Mar 28, 2015 at 19:46

1 Answer 1

1

Writing the code as:

while($lottery = mysql_fetch_array($query)){...}

would solve the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.