简体   繁体   English

使用mysql_fetch_assoc获取数组

[英]Fetch array with mysql_fetch_assoc

$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" “警告:array_rand()[function.array-rand]:第一个参数必须是第25行的public_html / php / lotterypick.php中的数组”
I have tried to replace mysql_fetch_assoc with mysql_fetch_array and that hasn't helped much either. 我试图用mysql_fetch_array替换mysql_fetch_assoc ,但也没有太大帮助。 Just given me the same error. 只是给了我同样的错误。 Any ideas? 有任何想法吗? Thanks. 谢谢。

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

Writing the code as: 将代码编写为:

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

would solve the problem. 会解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM