简体   繁体   English

在MySQL中插入多个JSON列(使用API​​)

[英]Insert multiple JSON column in MySQL (using API)

I'm trying to store sports data json column using API but I can't get it working properly. 我正在尝试使用API​​存储体育数据json列,但无法使其正常运行。 Though I can insert the data but it insert only the first column result and keep it duplicating on all the later columns. 虽然我可以插入数据,但它只插入第一列结果,并在所有后续列中保持重复。 At default, it shows 50 column upcoming football matches. 默认情况下,它将显示50列即将举行的足球比赛。 My problem is only the first line result is duplicating.. 我的问题是只有第一行结果重复。

Here is the code: 这是代码:

//connect to mysql db
$host = "localhost";
$user = "user";
$pass = "pass";
$db = "dname";
$conn = new mysqli($host, $user, $pass, $db) or die("ERROR:could not connect to 
    the database!!!");

//read the json file contents
$url = 'http://api.example.com/v1/events/upcoming?token=566645-
    sl1uDzY3tVvv9M&league_id=78&sport_id=1';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch) or die(curl_error($ch));

if ($data === false) {
    $info = curl_getinfo($ch);
    curl_close($ch);
    die('error occured during curl exec. Additioanl info: ' .
            var_export($info));
}
curl_close($ch);
$obj = json_decode($data, true);

foreach ($obj['results'] as $res) {
    $league = $res['league']['name'];
    $home = $res['home']['name'];
    $away = $res['away']['name'];
    $time = date("d/m h:i", $res['time']);
}
$sql = "INSERT INTO schedule(league, home, away, time)
    VALUES('$league', '$home', '$away', '$time')";
$result = mysqli_query($conn, $sql);
if (!$result) {
    die("ERROR :" . mysqli_error());
} else {
    echo "Records has been successfully inserted";
}
//close the connection
mysqli_close($conn);

The above code just insert the first json result, and it keeps repeating for the next column (result).. Help will be appreciated. 上面的代码只插入了第一个json结果,并且对下一个列(结果)保持重复。

You are facing the problem because you are executing mysql query outside the foreach loop. 您正面临问题,因为您正在foreach循环外执行mysql查询。 You have to execute the query inside the loop. 您必须在循环内执行查询。

Do something like 做类似的事情

foreach ($obj['results'] as $res) 
{ 
$league = $res['league']['name']; 
$home = $res['home']['name']; 
$away = $res['away']['name']; 
$time = date("d/m h:i", 
$res['time']);

$sql = "INSERT INTO schedule(league, home, away, time) VALUES('$league', '$home', '$away', '$time')";
$result=mysqli_query($conn,$sql); 
if(!$result) 
{ 
die("ERROR :". mysqli_error()); 
} else 
{
echo "Records has been successfully inserted";
 } //close the connection mysqli_close($conn);
}

Your insert query must be in foreach like below: 您的插入查询必须在foreach中,如下所示:

foreach ($obj['results'] as $res){
    $league = $res['league']['name'];
    $home = $res['home']['name'];
    $away = $res['away']['name'];
    $time = date("d/m h:i", $res['time']);

    $sql = "INSERT INTO schedule(league, home, away, time)
    VALUES('$league', '$home', '$away', '$time')";
    $result=mysqli_query($conn,$sql);
    if(!$result){
        die("ERROR :". mysqli_error());
    }else{
        echo "Records has been successfully inserted";
    }
}

Though the above code can resolve your issue but its better if you start using MySqli Prepared Queries to make it more secure. 虽然上面的代码可以解决您的问题,但是如果您开始使用MySqli Prepared Queries使其更安全,则更好。

Follow below link for more details: 请点击以下链接获取更多详细信息:

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

Put your insert query in foreach loop. 将插入查询放入foreach循环。 Because your variable replace with new foreach result. 因为您的变量替换为新的foreach结果。 Foreach reassign your veriables. Foreach将重新分配您的校验。

foreach ($obj['results'] as $res) {
    $league = $res['league']['name'];
    $home = $res['home']['name'];
    $away = $res['away']['name'];
    $time = date("d/m h:i", $res['time']);

    $sql = "INSERT INTO schedule(league, home, away, time)
    VALUES('$league', '$home', '$away', '$time')";
    $result = mysqli_query($conn, $sql);
    if (!$result) {
        die("ERROR :" . mysqli_error());
    } else {
        echo "Records has been successfully inserted";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM