如何在PHP中将数据从API插入MySQL

I want to insert the complete data from this API to the MySQL table using PHP. I had been able to insert it into the table. But the problem is that API's data is not completely inserting in the table, only one row of the data is inserting thricely.

这是代码:

<?php
//database connect
$connect = mysqli_connect("localhost","root","","test");

$data = file_get_contents("http://api.aladhan.com/v1/calendar?latitude=29.7604&longitude=-95.3698&method=2&year=2020&annual=true");

$array = json_decode($data,true);

foreach($array as $row)
{


     //Fetch the details 

     $date = $array['data']['1']['0']['date']['gregorian']['date'];
     $weekday = $array['data']['1']['0']['date']['gregorian']['weekday']['en'];
     $time_fajr = $array['data']['1']['0']['timings']['Fajr'];
     print_r($date); 

    //Insert the fetched Data into Database
    $sql = "INSERT INTO prayertime(Date, weekday, Fajr ) VALUES('$date', '$weekday', '$time_fajr')";


    mysqli_query($connect,$sql);

}

echo "data inserted";   ?>

输出显示以下内容:

01-01-202001-01-202001-01-2020data inserted

评论
  • wet
    wet 回复
       foreach($array as $row)
        {
    
    
             //Fetch the details 
    
             $date = $row['data']['1']['0']['date']['gregorian']['date'];
             $weekday = $row['data']['1']['0']['date']['gregorian']['weekday']['en'];
             $time_fajr = $row['data']['1']['0']['timings']['Fajr'];
             print_r($date); 
    
            //Insert the fetched Data into Database
            $sql = "INSERT INTO prayertime(Date, weekday, Fajr ) VALUES('$date', 
           '$weekday', '$time_fajr')";
    
    
            mysqli_query($connect,$sql);
    
        }