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?

LoveDroid
  • 75
  • 1
  • 10
  • try to `print_r($rows)` and see what it print – A l w a y s S u n n y Oct 05 '18 at 15:12
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – aynber Oct 05 '18 at 15:15
  • On the first iteration, you are assigning `$row` to the first element of `$rows`. `$row[1]` and `$row[2]` will not be populated yet, but you are trying to access them. – waterloomatt Oct 05 '18 at 15:15
  • I'm going to guess that `$rows` is empty before the loop. Therefore, on the first iteration, `$rows` will only have one row in key 0. The second iteration, it will have two rows with keys 0 and 1. – aynber Oct 05 '18 at 15:15
  • 1
    @don't angry me Array ( [0] => Array ( [score] => 10 ) [1] => Array ( [score] => 20 ) [2] => Array ( [score] => 30 ) ) – LoveDroid Oct 05 '18 at 15:16
  • Can you pls update your code to show _where_ you put `print_r($rows)`? – waterloomatt Oct 05 '18 at 15:17
  • @waterloomatt edited – LoveDroid Oct 05 '18 at 15:19
  • 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. – waterloomatt Oct 05 '18 at 15:30

1 Answers1

2

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);
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
  • Thank you so much for your help. 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); – LoveDroid Oct 05 '18 at 15:41
  • Sure. But are you sure you want to name your keys like `data`, `data1`, ...? You'll need some extra coding just do that. Looks like you're returning JSON. So why not just `echo json_encode($scores)`? – waterloomatt Oct 05 '18 at 15:43
  • Yes because the whole purpose of this code is to assign each value to those keys to use them on something else. That is why I am insisting on those keys. – LoveDroid Oct 05 '18 at 15:47