PHP: CSV import into MYSQL is always less than the actual amount of rows in the CSV file? -
this strange issue have , don't understand what's causing it.
basically, issue have simple upload function in php uploads csv file , imports each row mysql database.
now, issue have around 200+
rows in csv file when upload , import mysql using php page, around 158 of them imported
, don't errors @ either don't understand what's causing this.
i have csv file has around 300+ rows
in , when upload/import csv file, around 270 rows imported
mysql.
this import function short few rows , don't understand @ all.
this php import code:
error_reporting(-1); ini_set('display_errors', 'on'); if (isset($_post['up'])) { include "config/connect.php"; $imp= $_files["csv"]["name"]; move_uploaded_file($_files["csv"]["tmp_name"],"imports/$imp"); // path csv file located //////////////////////////////////////////////////////// define('csv_path',''); // name of csv file $csv_file = csv_path . "imports/".$imp.""; $i = 0; set_time_limit(10000); $fp = fopen("imports/".$imp."", "r"); while( !feof($fp) ) { if( !$line = fgetcsv($fp, 1000, ',', '"')) { continue; } $sql0 = "insert `mytable`(`column1`) values('".$line[0]."')"; $query0 = mysqli_query($db_conx, $sql0); } fclose($fp); printf("<script>location.href='mypage.php'</script>"); exit(); }
using direct import mysql out of question due security issues/hole..
could please advice on issue?
any appreciated.
to around quoting issues, want use prepared statements bind_param.
procedural style:
$stmt = mysqli_prepare($db_conx, "insert `mytable`(`column1`) values(?)"); mysqli_stmt_bind_param($stmt, 's', $line[0] ); mysqli_stmt_execute($stmt);
object-oriented style:
$stmt = $mysqli->prepare("insert `mytable`(`column1`) values(?)"); $stmt->bind_param('s', $line[0]); $stmt->execute();
per docs, use s
strings, i
integers, , d
doubles/decimals.
Comments
Post a Comment