这就是我从页面接收数据的方式:
$response = file_get_contents("https://api.themoviedb.org/3/movie/550?api_key=xxxxxx");
if ($response != FALSE) {
$response = json_decode($response, true);
}
以下是页面内容的示例:
{
"genres": [
{
"name": "Action"
},
{
"name": "Adventure"
},
{
"name": "Science Fiction"
}
]
}
以下是我插入数据的方法
$stmt = $conn->prepare("UPDATE genres SET genres_name = :genres_name WHERE tmdb_id = :tmdb_id");
$stmt->bindParam(':tmdb_id', $tmdb_id,PDO::PARAM_INT);
$stmt->bindParam(':genres_name', $genres_name,PDO::PARAM_INT);
if (isset($response["genres"]) && is_array($response["genres"]))
{
foreach ($response["genres"] as $genreObject)
{
$genres_name = $genreObject["name"];
$stmt->execute();
}
}
原始结果:
这是它在Genres
表
tmdb_id genres_name
5 Action
5 Action
5 Action
预期结果:
tmdb_id genres_name
5 Action
5 Adventure
5 Science Fiction