【问题标题】:Getting this error on new webhost Undefined offset: 1 and 2在新的虚拟主机上出现此错误未定义的偏移量:1 和 2
【发布时间】:2018-10-05 15:11:10
【问题描述】:

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

注意:未定义的偏移量:1($id2 行) 注意:未定义的偏移量: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);
?>

知道为什么吗?

【问题讨论】:

  • 尝试print_r($rows) 看看打印的内容
  • 在第一次迭代中,您将$row 分配给$rows 的第一个元素。 $row[1]$row[2] 将不会被填充,但您正在尝试访问它们。
  • 我猜$rows 在循环之前是空的。因此,在第一次迭代中,$rows 在键 0 中只有一行。第二次迭代时,它将有两行,键为 0 和 1。
  • @别生气我 Array ( [0] => Array ( [score] => 10 ) [1] => Array ( [score] => 20 ) [2] => Array ([分数] => 30))

标签: php mysql offset


【解决方案1】:

我认为这是设计有缺陷的症状。看起来您正在尝试查找 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. 
?>

编辑

您介意向我展示如何将数组结果分配给的示例吗 像这样的东西? $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);

【讨论】:

  • 非常感谢您的帮助。你介意向我展示如何将数组结果分配给这样的例子吗? $list['scores'] = array('data' => $id1, 'data1' => $id2, 'data2' => $id3);
  • 当然。但是您确定要将您的密钥命名为datadata1、...吗?你需要一些额外的编码才能做到这一点。看起来你正在返回 JSON。那么为什么不只是echo json_encode($scores)
  • 是的,因为这段代码的全部目的是将每个值分配给这些键,以便在其他东西上使用它们。这就是我坚持使用这些键的原因。
猜你喜欢
  • 1970-01-01
  • 2017-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
  • 2022-01-02
  • 2011-07-22
相关资源
最近更新 更多