在新的webhost未定义的偏移量1和2上获取此错误

h7wcgrx3  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(412)

我最近把我的脚本移到了一个新的网络主机,现在我得到了这个错误
注意:未定义偏移:1(第$id2行)注意:未定义偏移:2(第$id3行)
这是我的php代码

  1. <?php
  2. include '../connect_database.php';
  3. $sql = "SELECT score FROM dailyscore Where Id IN (1,2,3)";
  4. date_default_timezone_set('America/New_York');
  5. $result = $connect->query($sql);
  6. while ($row = mysqli_fetch_assoc($result)){
  7. $rows[] = $row;
  8. $id1= $rows[0]['score'];
  9. $id2= $rows[1]['score'];
  10. $id3= $rows[2]['score'];
  11. }
  12. $list['scores'] = array('data' => $id1, 'data1' => $id2, 'data2' => $id3);
  13. $myJSON = json_encode($list);
  14. echo $myJSON;
  15. print_r($rows);
  16. ?>

知道为什么吗?

a11xaf1n

a11xaf1n1#

我认为这是设计有缺陷的征兆。看来你在找3个球员的分数。您在3行上循环,但尝试访问每个迭代中所有3个玩家的数据。相反,您应该在每个玩家各自的迭代中访问他们的数据,并建立一个玩家数据列表。
为了直接回答您的问题,在迭代1中,您尝试访问元素0、1和2,但是 $rows 只填充了0。

  1. +-----------+-------------------------+--------------------------+
  2. | Iteration | You're trying to access | You only have access to |
  3. +-----------+-------------------------+--------------------------+
  4. | 1 | 0,1,2 | 0 |
  5. | 2 | 0,1,2 | 0,1 |
  6. | 3 | 0,1,2 | 0,1,2 |
  7. +-----------+-------------------------+--------------------------+

例子

  1. <?php
  2. // Turn on error reporting
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5. error_reporting(E_ALL);
  6. // Show MySQL errors as PHP exceptions
  7. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  8. include '../connect_database.php';
  9. // Build up this list inside the loop.
  10. $scores = [];
  11. $sql = "SELECT score FROM dailyscore Where Id IN (1,2,3)";
  12. date_default_timezone_set('America/New_York');
  13. $result = $connect->query($sql);
  14. while ($row = mysqli_fetch_assoc($result)){
  15. // You only have access to a single $row here.
  16. // Build up an array of the data you want instead of tring to access "other rows"
  17. $scores[] = $row['score'];
  18. }
  19. // Now, you can use $scores to build $list... or build $list inside the loop.
  20. ?>

编辑
你介意给我举个例子说明如何将数组结果分配给这样的对象吗$list['scores']=array('data'=>$id1,'data1'=>$id2,'data2'=>$id3);

  1. <?php
  2. // Turn on error reporting
  3. ini_set('display_errors', 1);
  4. ini_set('display_startup_errors', 1);
  5. error_reporting(E_ALL);
  6. // Show MySQL errors as PHP exceptions
  7. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  8. include '../connect_database.php';
  9. // Build up this list inside the loop.
  10. $scores = [];
  11. $counter = 0;
  12. $sql = "SELECT score FROM dailyscore Where Id IN (1,2,3)";
  13. date_default_timezone_set('America/New_York');
  14. $result = $connect->query($sql);
  15. while ($row = mysqli_fetch_assoc($result)){
  16. $keyName = 'data';
  17. if ($counter > 0) {
  18. $keyName = 'data' . $counter;
  19. }
  20. $scores[$keyName] = $row['score'];
  21. $counter++;
  22. }
  23. $list['scores'] = $scores;
  24. echo json_encode($list);
展开查看全部

相关问题