I have a JSON file with data output from another program and i am trying to import this data into a MySQL table with the following two issues.
Data never gets put in the table and no errors returned, when i echo out the output and enter it manually into the database it works fine.
The data changes from the JSON values when output into the array needed for the sql import string (Values).
JSON:
{
"version" : 90200,
"protocolversion" : 70002,
"blocks" : 322877,
"timeoffset" : -1,
"connections" : 49,
"proxy" : "",
"difficulty" : 34661425923.97693634,
"testnet" : false,
"relayfee" : 0.00001000,
"errors" : ""
}
PHP:
<?php
$data = file_get_contents("data.json");
$array = json_decode($data, true);
$keys = array_keys($array);
$rows = array();
foreach($keys as $key) {
$value = $array[$key];
$rows[] = "'" . $value . "'";
}
echo $rows;
$values = implode(",", $rows);
$hostname = 'localhost';
$username = 'username';
$password = 'password';
$database = 'database';
try
{
$dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
if($dbh)
echo 'Connected to database'; // echo a message saying we have connected
$datetime = date("Y-m-d H:i:s");
$count = $dbh->exec("INSERT INTO getinfo VALUES ('','" . $datetime . "'," . $values . "");
echo "Going into DB: ('','" . $datetime . "'," . $values . ")";
echo $count;// echo the number of affected rows
$dbh = null;// close the database connection
}
catch(PDOException $e)
{
print_r($e);
}
?>
Info ECHOED out and sent as VALUES:
('','2014-09-28 16:11:33','90200','70002','322880','-1','49','','34661425923.977','','1.0E-5','')
The first two values are manual entries i created for ID and DATE/TIME
As you can see the output is not the same as input however these values work with a manual INSERT query but not through this script also (hence the two problems).