duanfu1942 2018-10-05 15:11 采纳率: 100%
浏览 94
已采纳

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

I recently moved my script into a new webhost and now I am getting this error

Notice: Undefined offset: 1 (Line of $id2) Notice: Undefined offset: 2 (Line of $id3)

Here is my PHP code

<?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);
?>

Any idea why?

  • 写回答

1条回答 默认 最新

  • dongmen1860 2018-10-05 15:32
    关注

    I think this a symptom of a flawed design. Looks like you're trying to find the scores for 3 players. You're looping over the 3 rows but trying to access the data for all 3 players in each iteration. You should instead access each player's data in their respective iteration and build up a list of player data.

    To answer your question directly, on iteration 1 you are trying to access element 0, 1, and 2 but $rows is only populated with 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 |
    +-----------+-------------------------+--------------------------+
    

    Example

    <?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. 
    ?>
    

    EDIT

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?