Hi,

I have the following output json:

[
{
“user_id”: 999,
“user_uuid”: “xxxxxx”,
“first_name”: “xxxxx”,
“last_name”: “xxxxx”,
“gender”: null,
“email_address”: “xxxx@gmail.com”,
“country”: “XX”,
“date_of_birth”: null,
“stripe_id”: “xxxxxxxxxx”
}
]

I am getting t using below PHP:

$jsonData = array();

while($mysql_row = $mysql_query->fetch())
{
    $jsonData[] = $mysql_row;
}

echo json_encode($jsonData);

How can I append the following:

"authenticated": "true",

so the final output will be like this:

[
{
“user_id”: 999,
“user_uuid”: “xxxxxx”,
“first_name”: “xxxxx”,
“last_name”: “xxxxx”,
“gender”: null,
“email_address”: “xxxx@gmail.com”,
“country”: “XX”,
“date_of_birth”: null,
“stripe_id”: “xxxxxxxxxx”,
“authenticated”: “true”
}
]

    jassimalrahma Your example is a bit confusing -- you have an array that contains just one object. It looks like from your code that you might have more than one. If you want to add that authenticated=TRUE to each object in your array, put Weedpacket's code in your loop like so:

    while($mysql_row = $mysql_query->fetch())
    {
        $mysql_row['authenticated']  = 'true';
        $jsonData[] = $mysql_row;
    }

    You might also want to think about whether you want authenticated to be "true" (a string) or whether you want it to just be TRUE (a boolean value).

    sneakyimp while($mysql_row = $mysql_query->fetch())
    {
    $mysql_row['authenticated'] = 'true';
    $jsonData[] = $mysql_row;
    }

    if I try this I get:

    <br />
    <b>Fatal error</b>: Uncaught Error: Cannot use object of type stdClass as array in /home/softnames/oneid.live/api/v1/signin.php: 58
    Stack trace:
    #0 {main
    }

    on $mysql_row["authenticated"] row.

    Just to your note, I have a data coming from the database here:

    $mysql_query = $mysql_connection->prepare('CALL sp_go(:param_email)');
    $mysql_query->bindParam(':param_email', $email, PDO::PARAM_STR);
    $mysql_query->execute();
    
    $jsonData = array();
    
    while($mysql_row = $mysql_query->fetch())
    {
        $mysql_row['authenticated']  = 'true';
        
        $jsonData[] = $mysql_row;
    }

    and all I want is to add the ["authenticated"] element to it.

    Oh, so you're retrieving the rows as objects.

    $mysql_row->authenticated = 'true';

      jassimalrahma I It looks like your code is using some method, bindParam(), on the $mysql_query object that is an instance of some custom class rather than the official myqli classes, so I'm not really sure what sort of objects we are dealing with here. The mysqli pepare method returns a myqli_stmt object which has a method, bind_param (not bindParam() like your code) and if you want to call execute on that mysqli_stmt object, you must apparently call bind_result on that to specify the variables you want to use before you finally call fetch on that to iterate through the results. There are plenty of examples in the documentation.

      But, the error you have reported:

      jassimalrahma Fatal error</b>: Uncaught Error: Cannot use object of type stdClass as array in /home/softnames/oneid.live/api/v1/signin.php: 58

      Suggests that $mysql_row is an object, an instance of stdClass rather than an associative array. If that's the case, try this and it might help:

      while($mysql_row = $mysql_query->fetch())
      {
          $mysql_row->authenticated  = 'true';   
          $jsonData[] = $mysql_row;
      }
        Write a Reply...