Reputation: 21
I'm pulling information from our database (entered with GLua) 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
NULL bool(true)
Database contents:
[{"ArrestedBy":"James Scott","Date":1483732483,"Duration":60,"LeaveReason":"Jail time served.","ArrestReason":"test"}]
Any help will be appreciated!
After trying Darren's response:
I tried
$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:
["[{\"ArrestedBy\":\"James Scott\",\"Date\":1483732483,\"Duration\":60,\"LeaveReason\":\"Jail time served.\",\"ArrestReason\":\"test\"}]"] So I tried
echo $rap['ArrestedBy'];
and it returned
[
Upvotes: 2
Views: 462
Reputation: 13128
You're trying to access variables that aren't within the proper scope. $rapsheet
is only ever accessible within your while() {...
loop as it will be that current row of data. What you want to do is create $raps
as an array and insert the lines within it. From there you'll be able to json_decode()
as you please. Depending on how many rows you've got, you may need to loop over $raps
and decode each array element.
$raps = array();
while($rapsheet=mysql_fetch_assoc($rap)){
$raps[] = $rapsheet['value'];
};
// encode
$rap = json_encode($raps, TRUE);
Upvotes: 1