我已經搜索了關於這個問題的最後一個小時,但它變得非常討厭...PHP implode不能使用單引號?
反正,我試圖插入數據到一個mysqli分貝,我已經做了很多次,但之前,但由於某種原因,我無法弄清楚什麼錯誤......
<?php
require_once("../config/dbmetar.php");
$delimiter = ',';
$file = "metars.csv";
$db = new mysqli(DB_HOST_METAR, DB_USER_METAR, DB_PASS_METAR, DB_NAME_METAR);
$r = 0;
if (($handle = fopen($file, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 5000, $delimiter)) !== FALSE) {
if ($r >= 6) {
foreach($data as $i => $content) {
$data[$i] = $db->real_escape_string($content);
}
echo "INSERT INTO metars VALUES('" . implode("','", $data) . "');" . "<br>";
//$db->query("INSERT INTO metars VALUES('" . implode("','", $data) . "');");
}
$r++;
}
fclose($handle);
}
?>
的主要問題是與插入到metars值...值都應該用逗號分隔,周圍的每個字段值的單引號.. 。
目前這是echo輸出的內容:
INSERT INTO metars VALUES('PAJZ 011132Z AUTO 1 3/4SM BR FEW009 BKN019 OVC026 08/07 A2959 RMK AO2 PWINO TSNO P0001,PAJZ,2013-07-01T11:32:00Z,59.73,-157.27,8.0,7.0,,,,1.75,29.589567,,,TRUE,TRUE,,,TRUE,,TRUE,BR,FEW,900,BKN,1900,OVC,2600,,,IFR,,,,,,0.01,,,,,,SPECI,82.0');
有什麼建議嗎?
的var_dump($數據)返回這些:
array(1) { [0]=> string(238) "PAJZ 011132Z AUTO 1 3/4SM BR FEW009 BKN019 OVC026 08/07 A2959 RMK AO2 PWINO TSNO P0001,PAJZ,2013-07-01T11:32:00Z,59.73,-157.27,8.0,7.0,,,,1.75,29.589567,,,TRUE,TRUE,,,TRUE,,TRUE,BR,FEW,900,BKN,1900,OVC,2600,,,IFR,,,,,,0.01,,,,,,SPECI,82.0" } INSERT INTO metars VALUES('');
array(1) { [0]=> string(153) "CYOD 011131Z 18002KT 1/2SM FG FEW250 RMK CI0,CYOD,2013-07-01T11:31:00Z,54.4,-110.28,,,180,2,,0.5,,,,,,,,,,,FG,FEW,25000,,,,,,,LIFR,,,,,,,,,,,,SPECI,544.0" } INSERT INTO metars VALUES('');
array(1) { [0]=> string(276) "CYYD 011131Z AUTO VRB06KT 5SM -RA BR FEW007 BKN070 OVC084 15/14 A3005 RMK PRESRR PCPN 1.0MM PAST HR SLP173 DENSITY ALT 1900FT,CYYD,2013-07-01T11:31:00Z,54.82,-127.18,15.0,14.0,0,6,,5.0,30.050198,1017.3,,TRUE,,,,,,,-RA BR,FEW,700,BKN,7000,OVC,8400,,,MVFR,,,,,,,,,,,,SPECI,523.0" }
'var_dump($ data)',你可能實際上並沒有獲得多個值的數組。 – deceze
...其原因可能是某處的編碼問題,這意味着分隔符不匹配。 – deceze
「這些值應該全部用逗號分隔,每個字段值附帶單引號......」似乎只有字符串在輸入中被引用,而布爾值,日期和數字則不是。 – Powerlord