当可以通过URL访问CSV文件时,我可以解析CSV文件并创建100%正确的插入语句。问题是,当我在以fopen
结尾的网址上使用..format=csv
时,代码无效。
代码中是否有任何我缺少的工作?
<?php
$servername = "localhost";
$username = "root";
$password = "xxxx";
$dbname = "player";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$f_pointer=fopen("URL-format=csv","r");
$sql = "TRUNCATE TABLE scores";
$conn->query($sql);
while(! feof($f_pointer)){
$ar=fgetcsv($f_pointer);
$sql="INSERT INTO scores(pos,errorthing,name,total)values('$ar[0]','$ar[1]','$ar[2]','$ar[3]','$ar[4]')";
$conn->query($sql);
echo $sql;
echo "<br>";
}
?>
这是错误消息:
>root@MorryServer:/# php testing.php
PHP Notice: Undefined offset: 1 in /testing.php on line 21
PHP Notice: Undefined offset: 2 in /testing.php on line 21
PHP Notice: Undefined offset: 3 in /testing.php on line 21
PHP Notice: Undefined offset: 4 in /testing.php on line 21
INSERT INTO scores(pos,errorthing,name,total)values(??? UtUehOE_ZUzUAfAakQ87dsLbM.csvu??O????_a?t.????s??-??P?蜗+?L?r???ן??? Mj???g<?ӛ????_7??Q?D?Y?R?e?c','','','','')<br>PHP Notice: Undefined offset: 2 in /testing.php on line 21
PHP Notice: Undefined offset: 3 in /testing.php on line 21
PHP Notice: Undefined offset: 4 in /testing.php on line 21
INSERT INTO scores(pos,errorthing,name,total)values(' ??R?<???M??oCQ??0? P????[?*?q??EF?iB????','?EJsM???','','','')<br>PHP Notice: Undefined offset: 2 in /testing.php on line 21
PHP Notice: Undefined offset: 3 in /testing.php on line 21
PHP Notice: Undefined offset: 4 in /testing.php on line 21
4n c?%???o??'??q?wĠ!f1?)?d)ΐ??????z0?*total)values('R:
??','?g?ٓ4tG9????fd???*???','','','') <br>PHP Notice: Undefined offset: 4 in /testing.php on line 21
INSERT INTO scores(pos,errorthing,name,total)values('m?``???)??VZg?\{???@?P?}???gY9y?p?TA=ˌ','????@^Q?I~FR??%g??
*%;?M??.?&`??m?e cZy!????','4?xI??','#?????"}?.????u??d^????-e?dF?B','')<br>PHP Notice: Undefined offset: 3 in /testing.php on line 21
PHP Notice: Undefined offset: 4 in /testing.php on line 21
INSERT INTO scores(pos,errorthing,name,total)values('罡?m??7?&?ɣ
?g)??L3??l??T2?e[?v?
]!?????xm??ѧB?7?ļ?J?????ۋ?bA?»O?Y?4e?O??O?
cl??;???9-bZ?bط??Rk*t?????Ƣ??V??9??죨?\?۴ B??ԅ?T?a?OR? 3??P??????2~?}?F?8??c??LK@?(????Ns%T1??i]=M???=?????+??)W#{Z?b?S)μ???W??','?3vK?qZ?D?7mE/T?
?ѡ????5?~SMl?bwɿ8J?ؾ>??1Q?/e?ifäs/U?D????'YI??m;NU?%ی?fx??٦?x#??Ù??_??_??O????e??{M???e?v?E??~?)????_??B??I??X?','','')<br>PHP Notice: Undefined offset: 1 in /testing.php on line 21
PHP Notice: Undefined offset: 2 in /testing.php on line 21
PHP Notice: Undefined offset: 3 in /testing.php on line 21
PHP Notice: Undefined offset: 4 in /testing.php on line 21
INSERT INTO scores(pos,errorthing,name,total)values('??w՛"???B???Ep?? DXҖ4:?Z?oU<?`???w??"?*?Ϛ???/??'7Ɂ?;q"?*?2?E?65??S4
^??"?<҃
pl??}?g?Ksw?'xI?x?R??8[??%d??? 0-kp??="D??r}J?־?g??','','','','')<br>PHP Notice: Undefined offset: 2 in /testing.php on line 21
PHP Notice: Undefined offset: 3 in /testing.php on line 21
PHP Notice: Undefined offset: 4 in /testing.php on line 21
INSERT INTO scores(pos,errorthing,name,total)values('v??Mrpqӳq????x??m??z?Z?ɮ??}R͘???3t?:\0YxOyLv8?ؠi?Mi??<?>?T?KN?<?j??.?ZB?','???c?R>???R??i????b?Ģ????Nb??b<V>鄪???(??$?ɓ(????L>??(????Ƿ'?."?t??q?f꼪?\????N?IH?ӡ?;?Y?iO4J??Xʤ9??Ă?5?RUb \V.?;?????2??QN????????+??MX???F?k?*h?Olr?Q$?&^9H?v}K?F??8@G?y?T?Ԯ???W?1?{??<???ܷN?b^V8
?T?????\?nv4l?j??}???(?','','','')<br>PHP Notice: Undefined offset: 2 in /testing.php on line 21
PHP Notice: Undefined offset: 3 in /testing.php on line 21
PHP Notice: Undefined offset: 4 in /testing.php on line 21
INSERT INTO scores(pos,errorthing,name,total)values('`??wp??g???????B? u?SS??x?a^hKhv???51?JԸ8S???5zQ6?숵?`ntn?C*VMk?ؔ~|????Pw?)?IF?g?|MCt?gS????"V ?KXf>?^?6?]???????xW?W???gێ!??d?QoH??]?!??ia??`ͨ?>?-?h?ki?K ƅ?? ?!ZQ???t??? O???`??3??u??P??c܁?R?g???E?B3????@;????~?????8Gc?;?$sI?z??"??R5??N?;вX[?^? Aȃj??i??d','?[??0GUH?ٷ???c?*???Jl?V??a?j??lă?N:?&z?nL???m??=t??@W?O?:? S??????mPY??v?a똺?????P?p?=?#???]???ow?????GPp','','','')<br>
答案 0 :(得分:0)
在你的代码中,你没有得到$ ar [0],$ ar [1],$ ar [2]等的值,所以,你的错误。
试试这个,知道如何打开.csv文件,读取内容并插入数据库的各个列。
$row = 1;
if (($handle = fopen("xxxxxxxxx.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
$blackpowder = $data;
$dynamit = implode(";", $blackpowder);
$pieces = explode(";", $dynamit);
$col1 = $pieces[0];
$col2 = $pieces[1];
$col3 = $pieces[2];
$col4 = $pieces[3];
$col5 = $pieces[5];
mysql_query("
INSERT INTO `xxxxxx`
(`xxx`,`xxx`,`xxx`,`xxxx`,`xxx`)
VALUES
('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."')
");
}
}
}
答案 1 :(得分:0)
请勿使用while (!feof($f_pointer))
来读取文件。在尝试读取文件末尾的 之后,不会设置EOF标志。所以你正在读一个额外的时间,它会返回false
,当你尝试将它作为数组使用时会出现错误。
将循环更改为:
while ($ar = fgetcsv($f_pointer)) {
...
}