I'm trying to import data from a JSON feed using PHP into a MySQL database.
I have the code below but am not getting anywhere with it.
I keep just getting Connected to Database but nothing is being pulled in from the JSON data.
The JSON data is being created from a feed using import.io.
Any help appreciated
JSON data here
<?php
$data = file_get_contents('https://query.import.io/store/connector/e18543ae-48d1-47d3-9dc7-c3d55cab2951/_query?_user=363ec2db-fb95-413f-9a20-3fe89acbf061&_apikey=HOXvwSMX4HlmqH123i5HeELV6BwKq%2BFRInTzXc4nfl5VtP0pJyChxMT9AEiu1Ozi0vWZmUB%2BKcSsxHz2ElHNAg%3D%3D&format=JSON&input/webpage/url=http%3A%2F%2Fsports.yahoo.com%2Fgolf%2Fpga%2Fleaderboard');
$array = json_decode($data, true);
$rows = array();
$index = 0;
foreach($array['results'] as $mydata)
{
print_r($mydata);
echo "<br>";
foreach($mydata as $key => $value)
{
print_r ($key);
print_r ($value);
echo $index;
echo "<br><br>";
$rows[] = "('" . $value . "')";
}
echo "<br>";
$index++;
}
echo "<br><br><br>";
print_r ($rows);
$values = implode(",", $rows);
echo "<br><br><br>";
print_r ($values);
$hostname = 'localhost'; // write the rest of your query
$username = 'username';
$password = 'password';
try
{
$dbh = new PDO("mysql:host=$hostname;dbname=database", $username, $password);
echo 'Connected to database<br />'; // echo a message saying we have connected
$count = $dbh->exec("INSERT INTO import_io (total, round_1, round_2, round_3, round_4, thru, name/_source, name, today, name/_text, strokes) VALUES ($values)");
echo $count;// echo the number of affected rows
$dbh = null;// close the database connection
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
('0','Array') ('1','Array')... try to do aprint_r($values)to check the values you're trying to insert in the DBforeach($array['results'] as $key => $value) $rows[] = "('" . $key . "', '" . $value . "')";So this row is not working properly? I'm just wondering how to tackle it as I'm having trouble. Is it that the JSON has more results and I only want a particular part of them. i.e. the results section?$valuesbut it still won't populate. Any more help?('-'),('6:26 am'),('1:25 am'),('-'),('-'),('-'),('/golf/pga/players/David+Duval/12/scorecard/2014/29'),('http://sports.yahoo.com/golf/pga/players/David+Duval/12/scorecard/2014/29'),('-'),('David Duval'),('-'),('-'),('6:26 am'),('1:25 am'),('-'),('-'),('-'),('/golf/pga/players/David+Howell/565/scorecard/2014/29'),('http://sports.yahoo.com/golf/pga/players/David+Howell/565/scorecard/2014/29'),('-'),('David Howell'),('-'),..How do I get them formatted correctly for INSERT?