How to Insert the large json file into mysql with php?

user6476237

I want to read from large json file and store it in database with php.

I wrote this code for parsing and insert file but the last query does'nt work.

public function insert_from_json() {
    $json = '
            [            
            {
            "name": "The Adventurer",
            "year": 1917,
            "country": "USA",
            "durationMinutes": 24,
            "director": "Charles Chaplin"
            },
            {
            "name": "Mest kinematograficheskogo operatora",
            "year": 1912,
            "country": "Russia",
            "durationMinutes": 12,
            "director": "Wladyslaw Starewicz"
            }
            ]';

    $result = json_decode($json);

    $vals = '';
    foreach ($result as $key => $value) {

        if ($value) {
            $vals.="('$value->name','$value->year','$value->country','$value->durationMinutes','$value->director'),";
        }
    }
    $vals = trim($vals, ',');
    $stmt = $this->conn->prepare("INSERT INTO film (name,year,country,durationMinutes,director) VALUES ($vals)");
    $stmt->execute();
    echo 'successfully';
}

please help me for make it.

Mikey

Try this:

$json = '[            
    {
        "name": "The Adventurer",
        "year": 1917,
        "country": "USA",
        "durationMinutes": 24,
        "director": "Charles Chaplin"
    },
    {
        "name": "Mest kinematograficheskogo operatora",
        "year": 1912,
        "country": "Russia",
        "durationMinutes": 12,
        "director": "Wladyslaw Starewicz"
    }
]';

$result = json_decode($json);

$stmt = $this->conn->prepare("
    INSERT INTO film (name, year, country, durationMinutes, director) 
    VALUES (:name, :year, :country, :durationMinutes, :director)
");

// loop through each object
foreach ($result as $r) {
    // and insert it into the database
    $stmt->execute([
        ':name' => $r->name,
        ':year' => $r->year, 
        ':country' => $r->country, 
        ':durationMinutes' => $r->durationMinutes, 
        ':director' => $r->director
    ]);
}

The main difference here is that you are doing multiple inserts instead of one big insert.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to read really LARGE JSON file and input that file's data into a MYSQL database using node.js?

How to read really LARGE JSON file and insert that file's data into a MYSQL database using node.js?

How insert a string before last character in a large file

How to insert the image file in mysql table?

PHP Insert JSON to MySql Multitable

How to insert json to mysql database wp / php - numbers, strings and timestamp

Insert json in mysql database using php in codeigniter

Large mysql insert timeout

How to Insert Json Data in Mysql with PHP

How insert content of TXT file to JSON file

How to loop through PHP code to insert JSON data into MySQL database

How to send large json data (250kb) to mysql database using ajax javascript php

How do I insert multiple rows of JSON object data in mySQL using PHP PDO?

PHP - how to read json file header and other objects and insert in mysql

How to insert a large amount of XML into another XML file using python?

How to quickly insert large amounts of days into MySQL?

Mysql Insert Large data

how to insert xml to mysql with php

How to insert this data to mysql php

How to Insert JSON into mysql table with PHP

php: how to insert large form data into mysql

How to retrieve large data from mysql to php?

Use PHP to insert JSON to MySQL table

Insert large blocks of code into MySql

Insert JSON data into MySQL using PHP

Parse JSON file in PHP and insert into SQL ----

How do I extract data from JSON and insert it in MySQL with PHP?

Performance: Processing large JSON in js/php and storing it to MySQL or process it in MySQL

How to insert a pdf file in a MySQL database and then read it in php?