PHP Mysqli 脚本未定义偏移

我有一个脚本,它会在执行时检查 3 个随机行,我需要这 3 行有自己定义的值以供进一步使用。我编写的脚本可以正常工作,除了我为行变量获取了 Undefined offset: 1 和 Undefined offset: 2 。如果不是因为这些错误,我可以忽略它,但是我让脚本在设定的时间通过任务管理器运行,并且错误导致它无法完成。


我已经在研究并试图找到相关信息,但也许我没有在寻找正确的术语。


$query1 = mysqli_query($db, "SELECT szName, ip, dwRoleID FROM log_item, gamedata, `user` WHERE dwParam3 = '667287791' and gamedata.dwID = log_item.dwRoleID and gamedata.szName = `user`.`Name` GROUP BY `user`.ip ORDER BY RAND() LIMIT 3") or die(mysqli_error($db));

while ($row[] = mysqli_fetch_array($query1, MYSQLI_ASSOC)){


$id1 = $row[0]['dwRoleID'];

$id2 = $row[1]['dwRoleID'];

$id3 = $row[2]['dwRoleID'];

$Winner1 = $row[0]['szName'];

$Winner2 = $row[1]['szName'];

$Winner3 = $row[2]['szName'];


$query2 = mysqli_query($db, "DELETE FROM log_item WHERE dwRoleID IN ('$id1', '$id2', '$id3') and dwParam3 = '667287791'") or die(mysqli_error($db));


}


$msg = "Congratulations


$Winner1, $Winner2 and $Winner3 have won the giveaway.


Winners have 12 hours to claim their reward or a new winner will be picked.";

$curl = curl_init('https://');

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array('content' => $msg)));

curl_exec($curl);

我知道它的行输出格式是错误的,但除了错误之外,它完全按照我的意愿行事。


BIG阳
浏览 122回答 1
1回答

慕村225694

你做的 while 循环错了。在第一次迭代时,数组 $row 中索引 0 处将只有一个元素。这就是索引 1 和 2 缺失的原因。尝试这个:while ($row[] = mysqli_fetch_array($query1, MYSQLI_ASSOC)){    // empty}$id1 = $row[0]['dwRoleID'];$id2 = $row[1]['dwRoleID'];$id3 = $row[2]['dwRoleID'];$Winner1 = $row[0]['szName'];$Winner2 = $row[1]['szName'];$Winner3 = $row[2]['szName'];$query2 = mysqli_query($db, "DELETE FROM log_item WHERE dwRoleID IN ('$id1', '$id2', '$id3') and dwParam3 = '667287791'") or die(mysqli_error($db));更新 2018-08-10:改用 fetch_all(感谢 Dharman)$row = mysqli_fetch_all($query1, MYSQLI_ASSOC));
打开App,查看更多内容
随时随地看视频慕课网APP