我正在使用以下代碼將json代碼轉換爲數組並將值插入到mysql中。首先,我用一個for循環來創建這樣的表:mysql插入錯誤json解碼
$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!但是,錯誤仍然出現
$ I <=22 ---> $ I < COUNT($結果) – Lake