简体   繁体   中英

insert json response to mysql

I am trying to import a specific element of a json(display_name element as shown below), to my database.

At first i am innitiating curl:

    ..curl connection parameters
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl ,CURLOPT_SSL_VERIFYPEER, false);

    $response_data = curl_exec($curl);

    var_dump($response_data); //checking if response has data,it has: "{"sku":{"id":1234,"display_name":"Apple iPhone 8",..}
    curl_close($curl);
    if(!$response_data) {
    die('curl error is -' . curl_error($curl));

    $data = json_decode($response_data);

    foreach ($data as $product) {
        $displayname=$product->display_name;
        }

but nothing is stored and i dont have any errors:

$sql = "INSERT INTO phones (name)
VALUES ('$displayname')";

All about this :

$displayname=$product['display_name'];

is not an Object is an Array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM