使用php将JSON中的数据插入mysql
我想在mysql数据库中插入一个JSON。我的JSON是使用php将JSON中的数据插入mysql,php,mysql,sql,json,Php,Mysql,Sql,Json,我想在mysql数据库中插入一个JSON。我的JSON是 {"users": { "bert":6.44, "earnie":0.25, "bigbird":34.45 }} 我在mysql中有一个名为“USERSAMOUNTS”的表,它有两个冒号 这些列称为“用户名”和“金额” 我试图在一个查询中使用foreach循环和内爆将每个名称插入USERNAME列,将每个金额插入amount列。我使用的代码如下 $array = json_decode($data, true); $keys = a
{"users": { "bert":6.44, "earnie":0.25, "bigbird":34.45 }}
我在mysql中有一个名为“USERSAMOUNTS”的表,它有两个冒号
这些列称为“用户名”和“金额”
我试图在一个查询中使用foreach循环和内爆将每个名称插入USERNAME列,将每个金额插入amount列。我使用的代码如下
$array = json_decode($data, true);
$keys = array_keys($array); // get the value of keys
$rows = array(); // create a temporary storage for rows
foreach($keys as $key)
{ // loop through
$value = $array[$key]; // get corresponding value
$rows[] = "('" . $key . "', '" . $value . "')"; // add a row to the temporary storage
}
$values = implode(",", $rows); // 'glue' your rows into a query
$hostname = 'localhost'; // write the rest of your query
$username = 'usersname';
$password = 'userspassword';
try
{
$dbh = new PDO("mysql:host=$hostname;dbname=my_database", $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
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$array=json\u decode($data,true);
$keys=array_keys($array);//获取键的值
$rows=array();//为行创建临时存储
foreach($key作为$key)
{//循环通过
$value=$array[$key];//获取相应的值
$rows[]=“(“$key.”、“$value.””);//向临时存储器中添加一行
}
$values=内爆(“,”,$rows);//”将您的行粘贴到查询中
$hostname='localhost';//写下查询的其余部分
$username='usersname';
$password='userspassword';
尝试
{
$dbh=newpdo(“mysql:host=$hostname;dbname=myu数据库”、$username、$password);
echo“已连接到数据库”
;//回显一条消息,说明我们已连接
$count=$dbh->exec(“插入USERAMOUNTS(USERNAME,AMOUNT)值($VALUES)”);
echo$count;//回显受影响的行数
$dbh=null;//关闭数据库连接
}
捕获(PDO$e)
{
echo$e->getMessage();
}
…我的问题是,当我运行代码时,我得到的只是消息
已连接到数据库
以确认数据库连接。不会插入任何记录,也不会显示任何错误消息。这里有谁能指出我哪里出了问题,或者给我一个关于如何修复代码的提示吗?插入中不需要括号
$count = $dbh->exec("INSERT INTO USERAMOUNTS(USERNAME, AMOUNT) VALUES " . $values);
更改您的代码:
$array = json_decode($data, true);
$rows = array();
foreach($array['users'] as $key => $value)
$rows[] = "('" . $key . "', '" . $value . "')";
$values = implode(",", $rows);
$array = json_decode($data, true);
$keys = array_keys($array); // get the value of keys
$rows = array(); // create a temporary storage for rows
foreach($keys as $key)
{ // loop through
$value = $array[$key]; // get corresponding value
$rows[] = "('" . $key . "', '" . $value . "')"; // add a row to the temporary storage
}
最初的问题是,您的数组不是$array,而是$array[“用户”] 执行以下操作。 全部替换
$keys = array_keys($array); // get the value of keys
$rows = array(); // create a temporary storage for rows
foreach($keys as $key)
{ // loop through
$value = $array[$key]; // get corresponding value
$rows[] = "('" . $key . "', '" . $value . "')"; // add a row to the temporary storage
}
$values = implode(",", $rows); // 'glue' your rows into a query
以下是:
$values = Array();
foreach($array["users"] as $user=>$amount) {
$values[] = "('" . $user. "', '" . $amount. "')";
}
更少的代码-相同的结果。代码开头有问题:
$array = json_decode($data, true);
$rows = array();
foreach($array['users'] as $key => $value)
$rows[] = "('" . $key . "', '" . $value . "')";
$values = implode(",", $rows);
$array = json_decode($data, true);
$keys = array_keys($array); // get the value of keys
$rows = array(); // create a temporary storage for rows
foreach($keys as $key)
{ // loop through
$value = $array[$key]; // get corresponding value
$rows[] = "('" . $key . "', '" . $value . "')"; // add a row to the temporary storage
}
$array
是一个多维数组,只有一个元素的键为users
,因此您不是在数据上循环,而是在外部数组上循环。如果启用错误显示,您将在以下行中看到警告:PHP注意:数组到字符串转换
:
$rows[] = "('" . $key . "', '" . $value . "')";
您需要循环查看用户
子数组的内容:
foreach ($array['users'] as $key => $value) {
...
谢谢你的指针-当我用你的代码替换时,我只插入了一条记录。用户名有用户过账,金额默认为0。什么是[AMOUNT]的字段类型?字段类型为decimal(16,8)也很抱歉,我认为我不清楚-我没有用户名“users”-它位于json对象的开头-但我不需要存储它。