[英]Unable to add data to MYSQL database from JSON file using PHP
I'm new with PHP. 我是PHP新手。 I'v been struggling with this task for hours now.
我已经为这个任务苦苦挣扎了几个小时。 Earlier I used json_encode to get data from MYSQL to a JSON file.
之前,我使用json_encode将数据从MYSQL获取到JSON文件。 Now i try to revese and add the same data from JSON file to a new MYSQL database.
现在,我尝试重述并将相同数据从JSON文件添加到新的MYSQL数据库。 I have a problem with converting the array to string before passing it to MYSQL database tho.
在将数组传递到MYSQL数据库之前,我将数组转换为字符串时遇到问题。 The database works and I was able to add "players" there by inserting manual values instead of the $values from array.
数据库工作正常,我能够通过插入手动值而不是数组中的$ values在此添加“玩家”。 My code looks like this:
我的代码如下所示:
<?php
//open connection to mysql db
$con = mysqli_connect("localhost","root","","scoreboard2") or die("Error " . mysqli_error($con));
$scorefile = file_get_contents('scores.json');
$Score = json_decode($scorefile, true);
echo '<pre>' . print_r($Score, true) . '</pre>';
foreach ($Score as $field => $value) {
// Use $field and $value here
print_r($field . '=>' . $value . '<br/>', true);
//mysqli_query($con, "INSERT INTO scores (name, score, time) VALUES ($value->name, $value->score, $value->time)");
}
//mysqli_close($con);
?>
the JSON file looks like this: JSON文件如下所示:
[
{"id":"22",
"name":"Jack",
"score":"2142",
"time":"196:13",
"ts":"2016-02-23 15:36:23",
"date":"2016-02-23"},
{"id":"23",
"name":"Bob",
"score":"7026",
"time":"35:54",
"ts":"2016-02-23 15:40:33"}
]
etc.. and the "error" is this: 等等。“错误”是这样的:
Notice: Array to string conversion in F:\\XAMPP\\htdocs\\JSON_MySQL\\decode.php on line 13
注意:第13行的F:\\ XAMPP \\ htdocs \\ JSON_MySQL \\ decode.php中的数组到字符串的转换
Your issue is that you are converting the data in your .json file to an array by using parameter 2 of json_decode()
as true
. 您的问题是,您正在使用
json_decode()
参数2作为true
将json_decode()
文件中的数据转换为数组。
This is converting your objects to arrays and therefore the syntax you use in this line is wrong 这会将您的对象转换为数组,因此您在此行中使用的语法是错误的
mysqli_query($con, "INSERT INTO scores
(name, score, time)
VALUES ($value->name, $value->score, $value->time)");
because you are using object notation. 因为您正在使用对象表示法。
So change this line from 因此,从
$Score = json_decode($scorefile, true);
To 至
$Score = json_decode($scorefile);
SO 所以
?php
$con = mysqli_connect("localhost","root","","scoreboard2") or die("Error " . mysqli_error($con));
$scorefile = file_get_contents('scores.json');
$Score = json_decode($scorefile);
foreach ($Score as $object) {
mysqli_query($con, "INSERT INTO scores
(name, score, time)
VALUES ('{$object->name}', '{$object->score}', '{$object->time}')");
}
mysqli_close($con);
?>
Also note I quoted the values with single quotes and also wrapped the object properties in {}
which is required when using object or array notation inside a double quoted literal. 还要注意,我用单引号将值引起来,并将对象属性包装在
{}
,这是在双引号文字内使用对象或数组表示法时所必需的。
The error 错误
Notice: Array to string conversion in F:\\XAMPP\\htdocs\\JSON_MySQL\\decode.php on line 13
注意:第13行的F:\\ XAMPP \\ htdocs \\ JSON_MySQL \\ decode.php中的数组到字符串的转换
is produced by the line 由生产线生产
print_r($field . '=>' . $value . '<br/>', true);
(which actually is the 13th line) (实际上是第13行)
where you try to convert $value
which is an array (your second score object) to a string in order to concatenate it with the rest of the string 您尝试将作为数组(第二个得分对象)的
$value
转换$value
字符串,以便将其与字符串的其余部分连接
note that if you replace the error-producing line by 请注意,如果您将产生错误的行替换为
echo '<pre>' .$field . '=>' . print_r($value, true) . '</pre>'.'<br/>';
you get the 你得到
0=>Array
(
[id] => 22
[name] => Jack
[score] => 2142
[time] => 196:13
[ts] => 2016-02-23 15:36:23
[date] => 2016-02-23
)
1=>Array
(
[id] => 23
[name] => Bob
[score] => 7026
[time] => 35:54
[ts] => 2016-02-23 15:40:33
)
that you might originally expect 您最初可能期望的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.