0

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();
}
5
  • $jsonarray->result->quote->symbol Commented Feb 15, 2016 at 15:28
  • 1
    You should perhaps peruse the manual for how bind_param needs to be called: php.net/manual/en/mysqli-stmt.bind-param.php Commented Feb 15, 2016 at 15:28
  • Now that you've fixed the cause of the issue... is there still any problem...?! Commented Feb 15, 2016 at 15:48
  • I take that as a "no"?! You've basically removed your question with your last edits... Commented Feb 15, 2016 at 15:57
  • When I run var_dump($jsonarray); I get NULL I search now where the problem is. Commented Feb 15, 2016 at 16:02

3 Answers 3

2

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->...

Sign up to request clarification or add additional context in comments.

2 Comments

Yes thank you thats the problem, I am trying to search now for the reason
The JSON object has only one first level property: query. It's not {"count":1, "created": "today", "lang": "AZ", "results": {}}
2

Try with:

/* bind parameters for markers */
    $stmt->bind_param("ss", $jsonarray->result->{"quote"}->symbol, $jsonarray->result->{"quote"}->Volume);

Comments

1

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']
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.