如何将JSON数据添加到mysql数据中(如果可能的话)如何为其创建循环


How can I add JSON data into my mysql data if possible how can i create a loop for it

我正在使用一个api,它可以让我以json文件的形式获取它的游戏详细信息,我有所有的游戏id,但无法将其转换到我的msql数据库中,而且oyu可以告诉我如何创建所有id的循环来自动工作。这是我迄今为止制作的代码

//Get the id of the game
$gameid = "gu45bh84p";
//Call the api
$json=file_get_contents("http://www.example.com/?fetch=$gameid");
$details=json_decode($json);
//Check if respose contains the game information
if($details->Response=='True')
{  
//Print the game information
echo "game-ID : ".$details->gameID.'<br>';
echo "Title : ".$details->Title.'<br>';
echo "Year : ".$details->Year.'<br>';
echo "Poster Image Path: ".$details->Poster.'<br>';
echo "Released Date: ".$details->Released.'<br>';
echo "Genre : ".$details->Genre.'<br>';
echo "Language : ".$details->Language.'<br>';
echo "Country : ".$details->Country.'<br>';
}
//Show message if the game information is not returned by API
else
{
     echo "Game information not available.Please confirm game id";
}

实际上,为了不引起混淆,json_decode默认情况下会返回一个对象。您可以通过将true作为它的第二个参数来明确地要求它返回关联数组。

为了回答这个问题,您返回了一个对象,并且您似乎已经可以从中检索数据并将其显示在页面上。因此,如果您想要数据库中的数据,只需将这些值传递到数据库insert语句中即可。