I'm new with 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. Now i try to revese and add the same data from JSON file to a new MYSQL database. I have a problem with converting the array to string before passing it to MYSQL database tho. The database works and I was able to add "players" there by inserting manual values instead of the $values from array. My code looks like this:
我是PHP的新手。我现在几个小时都在努力完成这项任务。之前我使用json_encode将数据从MYSQL获取到JSON文件。现在我尝试将JSON文件中的相同数据添加到新的MYSQL数据库中。我有一个问题,在将数组传递给MYSQL数据库之前将数组转换为字符串。数据库工作,我能够通过插入手动值而不是数组中的$值来添加“播放器”。我的代码如下所示:
<?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中的数组到字符串转换
2 个解决方案
#1
1
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将.json文件中的数据转换为数组。
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.
另请注意,我使用单引号引用值,并在{}中包装对象属性,这在双引号文字中使用对象或数组表示法时是必需的。
#2
1
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);
print_r($ field。'=>'。$ value。'
',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(一个数组(您的第二个得分对象))转换为字符串,以便将其与字符串的其余部分连接起来
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
你可能原本期望的
#1
1
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将.json文件中的数据转换为数组。
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.
另请注意,我使用单引号引用值,并在{}中包装对象属性,这在双引号文字中使用对象或数组表示法时是必需的。
#2
1
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);
print_r($ field。'=>'。$ value。'
',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(一个数组(您的第二个得分对象))转换为字符串,以便将其与字符串的其余部分连接起来
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
你可能原本期望的