PHP: Writing CSV file to mysql inserts empty record -
this question has answer here:
i open csv file , trying write in mysql db.
it writes empty records , loop not break. below code
and throws error
1.warning: ,,fgetcsv() expects parameter 1 resource, string given in on line 19
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "ib"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("connection failed: " . $conn->connect_error); } $file=$_files["file"]["tmp_name"]; echo $file; $fh = fopen($file, 'r'); while (($emapdata = fgetcsv($file, 10000, ",")) !== false) { $sql = "insert ib(atmid,location,zone,cash,fault) values('$emapdata[0]','$emapdata[1] ','$emapdata[2]' ,'$emapdata[3]','$emapdata[4]')"; if ($conn->query($sql) === true) { echo "new record created successfully"; } else { echo "error: " . $sql . "<br>" . $conn->error; } } fclose($file); echo "csv file has been imported."; $conn->close(); ?>
wiki
Comments
Post a Comment