如何使用JSON在MySQL DB中插入大量数据

时间:2017-06-20 14:48:44

标签: php mysql json

我正在使用7个JSON文件,我试图一次插入95个coloums + 50-100行。

这就是我的代码的样子:我删除了一些6个JSON文件和其他行和列。这是我的代码的简短版本

<?php
$requestsDone = 2;
$maxRequests = 100;
$currentnumber = 1;
while ($requestsDone <= $maxRequests) {

    $response = file_get_contents("https://api.themoviedb.org/3/movie/".$requestsDone."?api_key=522cexxxxxxxxxxxxx9ax34a");
    if ($response != FALSE) {
        $response = json_decode($response, true);
    }

if (  ($response["title"]) != ""   )
{

        $requestsDone++;

include 'admin/connectdb.php';

try {

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO TABLE (title,image,money,tag1,tag2) 
    VALUES (:title, :image, :money,:tag1,:tag2)");
    $stmt->bindParam(':title', $title);
    $stmt->bindParam(':image', $image);
    $stmt->bindParam(':money', $money);
    $stmt->bindParam(':tag1', $tag1);
    $stmt->bindParam(':tag1', $tag2);

    // insert a row
    $movie_title = "".$response["title"]."";
if ( $response["image"] != ""        )
    $image = "".$response["image"]."";
if ( $response["money"] != ""        )
    $money = "".$response["money"]."";
if ( ($response["tag"][0]["name"]) != ""        )
    $tag1 = "".$response["tag"][0]["name"]."";
if ( ($response["tag"][1]["name"]) != ""        )
    $tag2 = "".$response["tag"][1]["name"]."";

    $stmt->execute();

    echo "New records created successfully ".$currentnumber++."  <br/>";
    }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
    $conn = null;

}

}
?>

我收到了2种错误:有时是第1次错误,有时是第2次错误。

第一次错误: Cloudflare time-out: Error 504 Screenshot

第二次错误: Error: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

如果您需要更多信息,请告诉我。

编辑:

这是其中一个JSON文件的样子

{"adult":false,"backdrop_path":"/87hTDiay2N2qWyX4Ds7ybXi9h8I.jpg","belongs_to_collection":null,"budget":63000000,"genres":[{"id":18,"name":"Drama"}],"homepage":"http://www.foxmovies.com/movies/fight-club","id":550,"imdb_id":"tt0137523","original_language":"en","original_title":"Fight Club","overview":"A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.","popularity":9.765478999999999,"poster_path":"/adw6Lq9FiC9zjYEpOqfq03ituwp.jpg","production_companies":[{"name":"Regency Enterprises","id":508},{"name":"Fox 2000 Pictures","id":711},{"name":"Taurus Film","id":20555},{"name":"Linson Films","id":54050},{"name":"Atman Entertainment","id":54051},{"name":"Knickerbocker Films","id":54052}],"production_countries":[{"iso_3166_1":"DE","name":"Germany"},{"iso_3166_1":"US","name":"United States of America"}],"release_date":"1999-10-15","revenue":100853753,"runtime":139,"spoken_languages":[{"iso_639_1":"en","name":"English"}],"status":"Released","tagline":"How much can you know about yourself if you've never been in a fight?","title":"Fight Club","video":false,"vote_average":8.199999999999999,"vote_count":8047}

0 个答案:

没有答案