Json数据被读取但没有使用php插入mysql

时间:2017-03-15 13:16:19

标签: php json

我试图使用php将json数据插入到mysql中,

我获得了成功消息,但没有插入记录。

我的json数据是:

jsondata.json:

{"users": { "bert":6.44, "earnie":0.25, "bigbird":34.45 }}

我的PHP代码:

<?php
//First: read data
$fo=fopen("data.json","r");
$fr=fread($fo,filesize("data.json"));
$array=json_decode($fr,true);
//Second: create $values
$rows = array();
foreach ($array['users'] as $key => $value)
$rows[] = "('" . $key . "', '" . $value . "')";
$values = implode(",", $rows);


 //To display all values from JSON file
 echo '<pre>';print_r($array);
//Save to DB
$hostname = 'localhost';                 
$username = 'root';
$password = '';

try 
{
$dbh = new PDO("mysql:host=$hostname;dbname=nodejs", $username, $password);
echo 'Connected to database<br />';
//$count = $dbh->exec("INSERT INTO USERSAMOUNTS(USERNAME, AMOUNT) VALUES " . $values) or die(print_r($dbh->errorInfo(), true)); 
$count = $dbh->exec("INSERT INTO json(firstName) VALUES " . $values) or die(print_r($dbh->errorInfo(), true)); 
echo $count;// echo the number of affected rows
$dbh = null;// close the database connection
echo 'Success<br />';
}
catch(PDOException $e)
{
  echo $e->getMessage();
}
?>

4 个答案:

答案 0 :(得分:0)

我认为问题可能是执行操作的顺序。

<?php

//First: read data
$fo = fopen("jsondata.json", "r");
$fr = fread($fo, filesize("jsondata.json"));
$array = json_decode($fr, true);

//Second: create $values
$rows = array();
foreach ($array['users'] as $key => $value)
    $rows[] = "('" . $key . "', '" . $value . "')";
$values = implode(",", $rows);

//Third: display
echo '<pre>';
print_r($array);

//Fourth: save to db
$hostname = 'localhost';
$username = 'root';
$password = '';

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=nodejs", $username, $password);
    echo 'Connected to database<br />'; // echo a message saying we have connected 
    $count = $dbh->exec("INSERT INTO USERAMOUNTS(USERNAME, AMOUNT) VALUES " . $values);
    echo $count; // echo the number of affected rows
    $dbh = null; // close the database connection
    echo 'Success<br />';
} catch (PDOException $e) {
    echo $e->getMessage();
}
?>

答案 1 :(得分:0)

启用或禁用预准备语句的模拟。有些驱动程序不支持本机准备好的语句或者对它们的支持有限,请查看 - http://php.net/manual/en/pdo.setattribute.php

$dbh = new PDO("mysql:host=$hostname;dbname=nodejs", $username, $password);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$count = $dbh->exec("INSERT INTO USERAMOUNTS(USERNAME, AMOUNT) VALUES " . $values);

希望得到这个帮助。

答案 2 :(得分:0)

问题在于您尝试插入数据的方式。我很惊讶你没有收到错误。

您应该使用准备好的声明。请参阅以下内容...... https://stackoverflow.com/a/4629088/2033178

答案 3 :(得分:0)

这里有些东西很时髦。

起初看起来你期望数据从$ data中神奇地传来(除非那是在某个地方传递的?)

$array = json_decode($data, true);
$rows = array();                                   
foreach($array['users'] as $key => $value)                         
$rows[] = "('" . $key . "', '" . $value . "')"; 
$values = implode(",", $rows);

然后看起来你正在打开一个文件并解析JSON(但没有使用$ rows []进行上述魔术)

$fo=fopen("jsondata.json","r");
$fr=fread($fo,filesize("jsondata.json"));
$array=json_decode($fr,true);

为什么不插入每个循环?

$fo=fopen("jsondata.json","r");
$fr=fread($fo,filesize("jsondata.json"));
$array=json_decode($fr,true);
$count = 0;
$dbh = new PDO("mysql:host=$hostname;dbname=nodejs", $username, $password);

try {
    foreach($array['users'] as $key => $value) 
    $count = $count + $dbh->exec("INSERT INTO USERAMOUNTS(USERNAME, AMOUNT) VALUES " . $key . " " . $value . ")";

 } catch ...