PHP Mysqli脚本未定义偏移

时间:2019-08-10 21:54:23

标签: 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);

我知道它的行输出格式是错误的,但是除了错误之外,它的工作完全是我想要的。

1 个答案:

答案 0 :(得分:0)

您正在执行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));