我使用A API从JSON文件插入数据。
现在,JSON文件将发送genres
。现在,一些JSON文件将发送2个类型的值,一些JSON文件将发送100个值。
所以,我不知道,如何插入它们。
请参阅此代码,这是我的代码的简短版本。目前,我的原始文件中的每个标题都有genres1
到genres100
个数字。但是在这里,为了简单起见,我只向genres1
显示genres5
。
所以,正如你所看到的,代码很长,如果我插入最多100个类型的值。如何缩短?使用Forloop还是什么?
<?php
$requestsDone = 1;
$maxRequests = 15;
while ($requestsDone <= $maxRequests) {
include 'logo/connectdb.php';
$requestsDone++;
$response = file_get_contents("https://api.themoviedb.org/3/movie/".($requestsDone-1)."?api_key=522cexxxxxxe0c834a");
if ($response != FALSE) {
$response = json_decode($response, true);
}
$firstname = "";
$lastname = "";
try {
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO TABLE (title, genres1,genres2,genres3,genres4,genres5)
VALUES (:title, :genres)");
$stmt->bindParam(':title', $title);
$stmt->bindParam(':genres1', $genres1);
$stmt->bindParam(':genres2', $genres2);
$stmt->bindParam(':genres3', $genres3);
$stmt->bindParam(':genres4', $genres4);
$stmt->bindParam(':genres5', $genres5);
// insert a row
$title = $response["title"];
if ( isset($response["genres"][0]["name"]) != "" )
$genres1 = $response["genres"][0]["name"];
if ( isset($response["genres"][1]["name"]) != "" )
$genres2 = "".$response["genres"][1]["name"]."";
if ( isset($response["genres"][2]["name"]) != "" )
$genres3 = "".$response["genres"][2]["name"]."";
if ( isset($response["genres"][3]["name"]) != "" )
$genres4 = "".$response["genres"][3]["name"]."";
if ( isset($response["genres"][4]["name"]) != "" )
$genres5 = "".$response["genres"][4]["name"]."";
$stmt->execute();
echo "New records created successfully";
}
}
?>
答案 0 :(得分:0)
以下是使用2个表而不是1个表的示例。
假设表(注意:genre_id或autoincremented id不在此表中)
movie
-----
tmdb_id | movie_title | budget
genre
-----------
genre_id (autoincrement primary key) | movie_id(foreign key) | genre_title
您的PHP看起来像:
<?php
$requestsDone = 1;
$maxRequests = 15;
while ($requestsDone <= $maxRequests) {
include 'logo/connectdb.php';
$requestsDone++;
$response = file_get_contents("https://api.themoviedb.org/3/movie/".($requestsDone-1)."?api_key=522cexxxxxxe0c834a");
if ($response != FALSE) {
$response = json_decode($response, true);
}
$firstname = "";
$lastname = "";
try {
$title = $response["title"];
$tmdb_id = $response["tbdb_id"]; //Or the correct name
$budget = $response["budget"];
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO movie (tmdb_id,movie_title,budget)
VALUES (:tmdbid,:title,:budget)");
$stmt->bindParam(':tmdbid', $title,PDO::PARAM_INT);
$stmt->bindParam(':title', $title,PDO::PARAM_STR);
$stmt->bindParam(':budget', $title,PDO::PARAM_INT);
// insert a row
$stmt->execute();
$stmt = $conn->prepare("INSERT INTO genre (movie_id,genre_title) VALUES (:genrefk,:title)");
$genre = "";
$stmt->bindParam(':genrefk', $tmdb_id,PDO::PARAM_INT);
$stmt->bindParam(':title', $genre);
if (isset($response["genres"]) && is_array($response["genres"])) {
foreach ($response["genres"] as $genreObject) {
$genre = $genreObject["name"];
$stmt->execute();
}
}
echo "New records created successfully";
}
}
?>
这是做什么的:
要获得与电影相关的所有类型,您需要执行以下操作:
SELECT * FROM movie JOIN movie_genre ON movie_genre.movie_id=movie.id;
注意:我假设您使用自动递增标识符,但您可以实际使用API提供的标识符。在这种情况下,您只需要相应地更改“thisId”和第一个插入。