Parsing json to enter into mysql

hi im trying to parse a json file to mysql

here is my code

[code]<?php
error_reporting(E_ALL);
ini_set(‘display_errors’, 1);
$json = file_get_contents(“http://192.168.1.104/testing_JSON/results.json”);

//results.json = line below

// {“gas”:[{“gas_id”:“2”,“value”:“123455”},{“gas_id”:“3”,“value”:“345554”}]}

$link = mysql_connect(‘localhost’, ‘root’, ‘root’);
if (!$link) {
die('Could not connect: ’ . mysql_error());
}
echo 'Connected successfully


';

mysql_select_db(‘db_home’, $link);

$result = json_decode($json);

echo $json .’


’;

foreach($result as $key => $value) {

if($value) {

mysql_query(“INSERT INTO table_gas (value) VALUES ($value->value)”);
}
mysql_close($link);

echo 'record updated';

}
?>[/code]

im getting the follwoing error Notice: Trying to get property of non-object in /var/www/testing_JSON/jsontomysql.php on line 23 // which is this line mysql_query(“INSERT INTO table_gas (value) VALUES ($value->value)”);

any suggestion would be gratefully appreciated

thanks

I’ve never attempt this myself, but try out
[php]mysql_query(“INSERT INTO table_gas (value) VALUES ($result->{‘value’})”);[/php]

are you decoding the json results?

thanks for the reply i get the following error by adding the code …

Catchable fatal error: Object of class stdClass could not be converted to string in /var/www/testing_JSON/jsontomysql.php on line 23

im not sure… im new to php… any suggestions … thanks

This works for me

[php]$results = json_decode($json, true);

echo $json .’


’;

foreach($results[“gas”] as $key => $value) {
if($value) {
//print $value[“value”].’


’;
mysql_query(“INSERT INTO table_gas (value) VALUES ($value[‘value’])”);
}[/php]

The issue was the decode was nestled within the “gas” tree of the array

thanks for the reply im getting the following error

PHP Parse error: syntax error, unexpected ‘’ (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /var/www/testing_JSON/jsontomysql2.php on line 21

The quotes are in the wrong place in the query, try

mysql_query(“INSERT INTO table_gas (value) VALUES ($value[value])”);

That’s assuming that whatever is being inserted is a number though.

Thanks for all the help got it working :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service