Как записать данные из json в mysql?

Добрый день пытаюcь из файла записывать данные в mysql ругается на 24 строку в цикле foreach
Файл json
{"tires":[
{"cae":"PXR0033103","price_sk3":10984,"price_sk3_rozn":13216,"rest_sk3":"более 40","name":"245/40R17 91S Blizzak VRX","tiretype":"Легковая","brand":"Bridgestone","model":"Blizzak VRX","width":245,"height":40,"diameter":"R17","diametr_out":0,"design":"R","speed_index":"S","load_index":"91","thorn_type":"","tonnage":"","side":"","runflat":"","usa":"","omolog":"","axle":"","season":"Зимняя","img_big_my":"http://api-b2b.pwrs.ru/31926/pictures/tyres/Bridgestone/Blizzak_VRX/src/big_0.png","market_model_id":"10557724","tech":"","protection":"","img_big_pish":"http://4tochki.ru/pictures/tyres/Bridgestone/Blizzak_VRX/big/0.png","img_small":"http://4tochki.ru/pictures/tyres/Bridgestone/Blizzak_VRX/small/0.png","brand_info":"http://4tochki.ru/descriptions/brands/bridgestone/info.html","model_info":"http://4tochki.ru/descriptions/models/bridgestone/Blizzak_VRX/info.html"},
{"cae":"R2377","price_mkrs":11962,"price":13158,"price_op_samara":11962,"price_op_samara_rozn":13158,"rest_mkrs":3,"rest_op_samara":4,"name":"285/50R20 112Q iceGuard Studless G075","tiretype":"Легковая","brand":"Yokohama","model":"iceGuard Studless G075","width":285,"height":50,"diameter":"R20","diametr_out":0,"design":"R","speed_index":"Q","load_index":"112","thorn_type":"","tonnage":"","side":"","runflat":"","usa":"","omolog":"","axle":"","season":"Зимняя","img_big_my":"http://api-b2b.pwrs.ru/31926/pictures/tyres/Yokohama/iceGuard_Studless_G075/src/big_0.png","market_model_id":"1779757099","tech":"","protection":"","img_big_pish":"http://4tochki.ru/pictures/tyres/Yokohama/iceGuard_Studless_G075/big/0.png","img_small":"http://4tochki.ru/pictures/tyres/Yokohama/iceGuard_Studless_G075/small/0.png","brand_info":"http://4tochki.ru/descriptions/brands/yokohama/info.html","model_info":"http://4tochki.ru/descriptions/models/yokohama/iceGuard_Studless_G075/info.html"}
]
}

Файл php
<?php
$servername = "localhost";
$username = "1";
$password = "1";
$dbname = "1";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$f_json = '16.json';


$json = file_get_contents("$f_json");

//$data = json_decode($fields_string, true); // return array not object


$obj = json_decode($json,true);

foreach($obj -> $tires as $item) { //foreach element in $arr
    $uses = $item['cae']; //etc price_sk3
  $s2=$item['price_sk3']; 
  $sql = "INSERT INTO test2020 (price, artikul) VALUES (".$uses.",".$s2." )";

	if ($conn->query($sql) === TRUE) {
    	echo "New record created successfully";
	} else {
    	echo "Error: " . $sql . "<br>" . $conn->error;
	}
}


$conn->close();
?>
  • Вопрос задан
  • 391 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы