[PHP] json_decode returning NULL [{},{}]

I’m working with the SantosRP gamemode.

I’m pulling information from our database with PHP, then using json_decode to change it to an array to work with but it’s returning NULL, whereas other ones are working?


// Get the RapSheet info from 'character_data_store'
$rap = mysql_query("SELECT * FROM `character_data_store` WHERE `character_id` = '$srp_uid' AND `key`='RapSheet'");
while($rapsheet=mysql_fetch_assoc($rap)){
    $raps = $rapsheet['value'];
};

Then I use


// Deal with the rapsheet JSON
echo $raps;
$raps = json_decode($rapsheet, true);
echo var_dump($raps, TRUE);

The echo’s are to check that it’s working, the information is being pulled successfully as it echos, although the var_dump returns

Database contents:

Someone from StackOverflow suggested the below:


$rap = mysql_query("SELECT * FROM `character_data_store` WHERE `character_id` = '$srp_uid' AND `key`='RapSheet'");
$raps = array();
    while($rapsheet=mysql_fetch_assoc($rap)){
    $raps[] = $rapsheet['value'];
};

// encode
$rap = json_encode($raps, TRUE);
echo $rap;

And that returned:

So I tried:


echo $rap['ArrestedBy'];

and it returned

Any help would be appreciated

First, this isn’t the section for PHP. That’s the programming section.
Second, I believe it’s stored in the database as an array of JSON objects. Try



echo $rap[0]['ArrestedBy'];


instead.

Oh, I wasn’t aware that it wasn’t allowed.

I tried that, it just echoed ‘[’

Try running print_r($rap);

Just returns the same as echoing $rap

Oh, my bad. I think you want:



$rap = json_decode($raps[0], true);


instead of the json_encode line.

Thanks! I’ve used this below and it’s working…


$rap = json_decode($raps[0], TRUE);
echo $rap[0]['ArrestedBy'];

You should try out pdo instead of mysqli, it’s much more secure.

mysqli has parameterized queries, and is fine if used correctly. PDO is useful because of its multiple driver support, not its security. You’re thinking mysql vs PDO or vs mysqli.
Though, PDO is simpler, which would make it better for quality-of-life.