gpt4 book ai didi

php - 在新的虚拟主机上出现此错误 undefined offset : 1 and 2

转载 作者:行者123 更新时间:2023-11-29 02:11:00 26 4
gpt4 key购买 nike

我最近将我的脚本移到了一个新的虚拟主机中,现在我遇到了这个错误

注意: undefined offset :1($id2 的行)注意: undefined offset :2($id3 的行)

这是我的 PHP 代码

<?php
include '../connect_database.php';

$sql = "SELECT score FROM dailyscore Where Id IN (1,2,3)";
date_default_timezone_set('America/New_York');
$result = $connect->query($sql);
while ($row = mysqli_fetch_assoc($result)){
$rows[] = $row;
$id1= $rows[0]['score'];
$id2= $rows[1]['score'];
$id3= $rows[2]['score'];

}

$list['scores'] = array('data' => $id1, 'data1' => $id2, 'data2' => $id3);

$myJSON = json_encode($list);

echo $myJSON;
print_r($rows);
?>

知道为什么吗?

最佳答案

我认为这是设计缺陷的表现。看起来您正在尝试查找 3 位玩家的分数。您正在遍历 3 行,但试图在每次迭代中访问所有 3 名玩家的数据。相反,您应该在各自的迭代中访问每个玩家的数据,并建立一个玩家数据列表。

要直接回答您的问题,在迭代 1 中您尝试访问元素 0、1 和 2,但 $rows 仅填充了 0。

+-----------+-------------------------+--------------------------+
| Iteration | You're trying to access | You only have access to |
+-----------+-------------------------+--------------------------+
| 1 | 0,1,2 | 0 |
| 2 | 0,1,2 | 0,1 |
| 3 | 0,1,2 | 0,1,2 |
+-----------+-------------------------+--------------------------+

例子

<?php
// Turn on error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Show MySQL errors as PHP exceptions
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

include '../connect_database.php';

// Build up this list inside the loop.
$scores = [];

$sql = "SELECT score FROM dailyscore Where Id IN (1,2,3)";
date_default_timezone_set('America/New_York');
$result = $connect->query($sql);
while ($row = mysqli_fetch_assoc($result)){
// You only have access to a single $row here.
// Build up an array of the data you want instead of tring to access "other rows"
$scores[] = $row['score'];
}

// Now, you can use $scores to build $list... or build $list inside the loop.
?>

编辑

Do you mind to show me example how to assign the array results to something like this? $list['scores'] = array('data' => $id1, 'data1' => $id2, 'data2' => $id3);

<?php
// Turn on error reporting
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Show MySQL errors as PHP exceptions
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

include '../connect_database.php';

// Build up this list inside the loop.
$scores = [];
$counter = 0;

$sql = "SELECT score FROM dailyscore Where Id IN (1,2,3)";
date_default_timezone_set('America/New_York');
$result = $connect->query($sql);
while ($row = mysqli_fetch_assoc($result)){

$keyName = 'data';

if ($counter > 0) {
$keyName = 'data' . $counter;
}

$scores[$keyName] = $row['score'];

$counter++;
}

$list['scores'] = $scores;

echo json_encode($list);

关于php - 在新的虚拟主机上出现此错误 undefined offset : 1 and 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52668626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com