1

我已经搜索了关于这个问题的最后一个小时,但它变得非常烦人......

无论如何,我正在尝试将数据插入到 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($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" } 
4

2 回答 2

2

这意味着 $data 有 1 个项目,因为 implode 以这种方式工作得很好。

var_dump($data);

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

于 2013-07-01T14:46:26.457 回答
-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','');

回声内爆("','", $a);

结果是: 在此处输入图像描述

它工作正常!

我的 php 版本是:PHP 5.3.19 (cli) (built: Apr 30 2013 21:16:28) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

你可以在 php.net 中找到它http://www.php.net/manual/zh/function.implode.php

所以,检查你的 php 版本!

于 2013-07-01T14:58:37.927 回答