PHP内爆不使用单引号?

时间:2013-07-01 14:42:20

标签: php mysql 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);
}

?>

主要问题是插入元数值...值应全部用逗号分隔,并在每个字段值周围用单引号...

目前这是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($ data)返回了这些:

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" } 

2 个答案:

答案 0 :(得分:2)

这意味着$ data有1个项目,因为implode在这方面工作正常。

var_dump($data);

请运行转储。也许你有错误的分隔符,所以$ data只有一个元素。

答案 1 :(得分:-1)

我试试这些代码:

$a = array('PAJZ 011132Z AUTO 1 3/4SM BR FEW009 BKN019 OVC026 08/07 A2959 RMK AO2 PWINO TSNO P0001','PAJZ', '2013-07-01T11:32:00Z','','a','b','');

echo implode(“','”,$ a);

结果是:  enter image description here

一切正常!

我的php版本是: PHP 5.3.19(cli)(内置:2013年4月30日21:16:28) 版权所有(c)1997-2012 PHP小组 Zend Engine v2.3.0,版权所有(c)1998-2012 Zend Technologies

你可以在php.net http://www.php.net/manual/zh/function.implode.php

中找到它

所以,检查你的php版本!