Reputation: 406
I am trying to get the value of two variables from an json array. The array becomes sended with ajax then decoded and "saved" into $jsonarray
. Then I try to get the volume
and the symbol
variables from the array and insert them into my database. I dont understand the syntax of this $jsonarray->result->{"quote"}->symbol
and tried any times how its right but the error wont disappear.
thats my array:
{"query":{"count":1,"created":"2016-02-15T15:11:47Z","lang":"de-DE","results":{"quote":{"symbol":"ZN","Ask":"2.05","Bid":"1.78","Volume":"13214","PercentChange":"+0.56%"}}}}
relevant php piece:
$jsonString = $_POST['mydata'];
$jsonarray = json_decode($jsonString[0]['query']);
if ($stmt = $mysqli->prepare('INSERT INTO volume (stocksymbol, volume, time) VALUES ( ?, ?, now())')) {
/* bind parameters for markers */
$stmt->bind_param("si", $jsonarray->result->{"quote"}->symbol, $jsonarray->result->{"quote"}->Volume);
/* execute query */
$stmt->execute();
/* close statement */
$stmt->close();
}
Upvotes: 0
Views: 1020
Reputation: 1342
You could try and decode your JSON as an associative array instead. Assuming that $_POST['mydata']
contains the JSON string you showed us, try this:
$jsonString = $_POST['mydata'];
$jsonarray = json_decode($jsonString, TRUE);
This way, you can access the values in a more consistent way:
$stmt->bind_param(
"si",
$jsonarray['query']['results']['quote']['symbol'],
$jsonarray['query']['results']['quote']['Volume']
);
Upvotes: 1
Reputation: 864
Are you sure this line is correct?
$jsonarray = json_decode($jsonString[0]['query']);
In this case, you should access to the result by:
$jsonarray->query->results->...
Upvotes: 2
Reputation: 2167
Try with:
/* bind parameters for markers */
$stmt->bind_param("ss", $jsonarray->result->{"quote"}->symbol, $jsonarray->result->{"quote"}->Volume);
Upvotes: 2