php - mysql插入错误json解码

标签 php mysql json

我正在使用以下代码将 json 代码转换为数组并将值插入到 mysql 中。首先,我使用 for 循环创建如下表:

$url='http://www.coinchoose.com/api.php';
$contents = file_get_contents($url); 
$contents = utf8_encode($contents); 
$results = json_decode($contents, true); 

for ($i=0; $i<=22; $i++){

mysql_query("CREATE TABLE $symbol(
id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), timestamp BIGINT, symbol VARCHAR(3), name VARCHAR(20), algo VARCHAR(20), currentBlocks VARCHAR(20), difficulty DECIMAL (18,9), reward DECIMAL (18,9), price DECIMAL (18,9), exchange VARCHAR(20), ratio DECIMAL (8,4))")
or die(mysql_error()); 

} 

然后我使用另一个 for 循环将 json api 中的值插入 http://www.coinchoose.com/api.php进入 mysql 表,如下所示:

$url='http://www.coinchoose.com/api.php';
$contents = file_get_contents($url); 
$contents = utf8_encode($contents); 
$results = json_decode($contents, true); 
print_r($results);

$time=time();

for ($i=0; $i<=22; $i++){
$symbol=strtolower($results[$i]['symbol']);
$name=$results[$i]['name'];
$algo=$results[$i]['algo'];
$currentBlocks=$results[$i]['currentBlocks']; 
$difficulty=$results[$i]['difficulty'];
$reward=$results[$i]['reward']; 
$price=$results[$i]['price'];
$exchange=$results[$i]['exchange']; 
$ratio=$results[$i]['ratio'];

mysql_query("INSERT INTO $symbol VALUES (id, $time, '$symbol', '$name', '$algo', '$currentBlocks',  $difficulty, '$reward', $price, '$exchange', $ratio)") or die(mysql_error());  

}

我收到以下错误,但我不明白:

( ! ) Notice: Undefined offset: 21 in C:\wamp\www\api.php on line 45
Call Stack
#   Time    Memory  Function    Location
1   0.0004  706424  {main}( )   ..\api.php:0

( ! ) Notice: Undefined offset: 21 in C:\wamp\www\api.php on line 46
Call Stack
#   Time    Memory  Function    Location
1   0.0004  706424  {main}( )   ..\api.php:0

( ! ) Notice: Undefined offset: 21 in C:\wamp\www\api.php on line 47
Call Stack
#   Time    Memory  Function    Location
1   0.0004  706424  {main}( )   ..\api.php:0

( ! ) Notice: Undefined offset: 21 in C:\wamp\www\api.php on line 48
Call Stack
#   Time    Memory  Function    Location
1   0.0004  706424  {main}( )   ..\api.php:0

( ! ) Notice: Undefined offset: 21 in C:\wamp\www\api.php on line 49
Call Stack
#   Time    Memory  Function    Location
1   0.0004  706424  {main}( )   ..\api.php:0

( ! ) Notice: Undefined offset: 21 in C:\wamp\www\api.php on line 50
Call Stack
#   Time    Memory  Function    Location
1   0.0004  706424  {main}( )   ..\api.php:0

( ! ) Notice: Undefined offset: 21 in C:\wamp\www\api.php on line 51
Call Stack
#   Time    Memory  Function    Location
1   0.0004  706424  {main}( )   ..\api.php:0

( ! ) Notice: Undefined offset: 21 in C:\wamp\www\api.php on line 52
Call Stack
#   Time    Memory  Function    Location
1   0.0004  706424  {main}( )   ..\api.php:0

( ! ) Notice: Undefined offset: 21 in C:\wamp\www\api.php on line 53
Call Stack
#   Time    Memory  Function    Location
1   0.0004  706424  {main}( )   ..\api.php:0


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VALUES (id, 1369998276, '', '', '', '', , '', , '', )' at line 1

我希望有人能澄清为什么我会收到此错误。任何关于更好地编码上述内容的建议,因为我确信它可以以更漂亮/更好的方式完成,我们非常感激。该代码确实有效,因为这些值被解析到 mysql 中!但是仍然出现错误

最佳答案

您的 for 循环条件错误。

for ($i = 0; $i < 22; $i++ ) {    // Notice the `<` and not `<=`
<小时/>

或者,按照 comments 中的建议:

for ($i = 0; $i < count($result); $i++ ) {

关于php - mysql插入错误json解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16856006/

相关文章:

php - 从基于 REST 的 Web 服务调用 C++ 函数

php - MailChimp 支持 : the final "welcome" email with the link to unsubscribe

javascript - 如何修复我的建筑树功能。如果 parent ID 不存在

mysql - 获取企业架构数据库的 SQL 查询

javascript - 在javascript中动态解析json对象

jquery - 如何通过ajax从struts2操作方法获取json结果(使用jquery)

遇到PHP7非数值

PHP 和 XML。使用 PHP 遍历 XML 文件

php - 在 POST 复选框值的数组上使用 preg_match

php - 在进行 mysql 查询时将 php 变量设置为无穷大