0

I am trying to get json values but I get :

Warning: Illegal string offset 'data_id' in /home/myFiles/thepage.com/djs.php on line 17

Warning: Illegal string offset 'data_key' in /home/myFiles/thepage.com/djs.php on line 17

include("func/cnfng.php");
include_once 'func/func.php'; // Functions 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$Ups = new THE_UPDATES($db);
$TheData =  $Ups->Data();

$sql = mysqli_query($db,"SELECT * FROM myData") or die(mysqli_error($db));

$arr= [];
$row = mysqli_fetch_array($sql, MYSQLI_ASSOC);

foreach($row as $data) { 
    $arr[] = [
        $data['data_id'] => $TheData[$data['data_key']]['English'] 
        // ^
        // |__ This is on line 17
    ];
} 
echo json_encode($arr);

Can you help me with the solution, please.

AlwaysStudent
  • 1,354
  • 18
  • 49
  • @user3783243 var_dump($arr) is `string(52) "[{"1":"Pages"},{"l":null},{"L":null},{"D":null}]"` and var_dump($data); is `string(1) "1" string(9) "pages" string(9) "Pages" string(6) "Pagos"` it looks like just showing first row there is 600 row from database. – AlwaysStudent Aug 23 '20 at 11:45
  • @user3783243 updated question with more details. – AlwaysStudent Aug 23 '20 at 11:58
  • 1
    `var_dump($arr)` should not have been provided. That is after the error and unrelated. What does `foreach($sql as $data) {` give back? – user3783243 Aug 23 '20 at 12:29
  • @user3783243 Now I got all results without any error. – AlwaysStudent Aug 23 '20 at 12:35
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Aug 23 '20 at 13:25

2 Answers2

0

The $row = mysqli_fetch_array($sql, MYSQLI_ASSOC); returns 1 row per call. This means $row in your example only ever has 1 row (or should).

The fetch method isn't required with mysqli because the query function returns a result object that is iterable. You just need to change your foreach to use the return and remove the fetch.

foreach($sql as $data) {

also might want to change the variable to $result since it is a result and not SQL.

I'm not clear why/how the fetch -> foreach changed the returned data.

user3783243
  • 5,368
  • 5
  • 22
  • 41
0

To me it looks like your issue is that json_encode is trying to encode a php associative array into a json array which doesn't work because json arrays require that the indexes be numeric stating with 0 incrementing by 1, no gaps allowed.

you have two options, either convert to object the php array like so

foreach($row as $data) { 
    $arr[] = (object) [
        $data['data_id'] => $TheData[$data['data_key']]['English'] 
        // ^
        // |__ This is on line 17
    ];
} 
echo json_encode($arr);

or use numeric indexes json requires (0,1,2,3,...) like so

foreach($row as $data) { 
    $arr[] = [
        $data['data_id'],
        $TheData[$data['data_key']]['English'] 
        // ^
        // |__ This is on line 17
    ];
} 
echo json_encode($arr);
Nathanael
  • 870
  • 5
  • 11