I can parse a CSV file and create Insert statements 100% correct when a CSV file is accessible via URL. The issue is that the code does not work when I use use fopen
on a URL ending in ..format=csv
.
Is there anything I am missing in the code for this to work?
<?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>";
}
?>
This is the error message:
>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#
In your code you are not getting values for $ar[0],$ar[1],$ar[2] etc, so, your are getting errors.
try this, to know, how to open a .csv file, read the contents and inserting into respective columns of the database.
$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>
";
$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."')
");
}
}
}
Don't use while (!feof($f_pointer))
to read a file. The EOF flag isn't set until after you try to read at the end of file. So you're reading an extra time, which returns false
, and when you try to use that as an array you get errors.
Change the loop to:
while ($ar = fgetcsv($f_pointer)) {
...
}