user2452426
user2452426

Reputation: 1061

PHP Json decode is ok, but decoded values are void

I used this code to decode json, sent with curl (POST):

$json_obj = json_decode( file_get_contents('php://input')); // JSON as obj
var_dump($json_obj);
$id_a = $json_obj -> {'$id_a'}; 
$id_b =  $json_obj -> {'$id_b'};
$value = $json_obj -> {'$value'};

It should works fine, because var_dump() says:

object(stdClass)#1 (3) {
  ["id_a"]=>
  int(1)
  ["id_b"]=>
  int(1)
  ["value"]=>
  float(89.35)
}

Then I use these vars to make a sql query:

$sqlcmd = "INSERT INTO TABLE_1 (ID_A, ID_B, VALUE) 
          VALUES  (".$id_a.", ".$id_b.", ".$value.")";

I pass it to mysql_query(), but here it is how it reads $sqlcmd:

INSERT INTO TABLE_1 (ID_A, ID_B, VALUE)
VALUES  (, , )

Void values...

Any hint? Where I am mistaking? Thanks in advance

Upvotes: 0

Views: 89

Answers (2)

Eric Wich
Eric Wich

Reputation: 1544

You have extra $ signs in your code such as:

$id_a = $json_obj -> {'$id_a'};

Try this instead:

$id_a = $json_obj -> {'id_a'}; 
$id_b =  $json_obj -> {'id_b'};
$value = $json_obj -> {'value'};

Upvotes: 0

Schleis
Schleis

Reputation: 43770

You aren't setting the variables correctly.

$id_a = $json_obj->id_a; 
$id_b =  $json_obj->id_b;
$value = $json_obj->value;

Upvotes: 1

Related Questions