I need to insert my CSV data into mysql by insert query. Currently my CSV having 9976 rows data. But after running the query 9-10 lines skips randomly after 1000 rows and very hard to find out that which rows was skipped.
Firstly I tried to access the folder, then accessing the file and taking the csv first row as my header for table. Then running the query to insert the data in mysql but it skipped some random rows.
$dir = "C:\Users\\".strtolower($username)."\Downloads";
$fp = opendir($dir);
$dates = array();
$latest_file = glob($dir."\\Filter_ Tempo-jql-AP*");
closedir($fp);
$filepath=$latest_file[0];
$the_big_array = [];
$tablename="aht_tracker";
$dbname="ford_resource_capacity";
$conn =mysqli_connect("localhost","root","","$dbname") or die(mysqli_connect_error());
$fields="";
$fields1="";
$fieldsinsert="";
if (($h = fopen("{$filepath}", "r")) !== FALSE)
{
if (($data = fgetcsv($h, 100000, ",")) !== FALSE)
{
$issuekey=array_search("Issue key", $data);
$hours=array_search("Hours", $data);
$username=array_search("Username", $data);
$issuetype=array_search("Issue Type", $data);
$workdescription=array_search("Work Description", $data);
$new_array=array($issuekey,$hours,$username,$issuetype,$workdescription);
$arr_count=count($new_array);
$c=0;
$fieldsinsert .='(';
foreach ($new_array as $key => $value)
{
$fieldsinsert .=($key==0) ? '' : ', ';
$fieldsinsert .="`".str_replace(" ","_",$data[$value])."`";
$fields .="`".str_replace(" ","_",$data[$value])."` varchar(250) DEFAULT NULL,";
}
$fieldsinsert .= ')';
}
while(($data = fgetcsv($h, 100000, ",")) !== FALSE)
{
$fieldsInsertvalues="";
$c=0;
foreach ($new_array as $key => $value)
{
$fieldsInsertvalues .=($key==0) ? '(' : ', ';
$fieldsInsertvalues .="'".$data[$value]."'";
}
$fieldsInsertvalues .= ')';
$sql1 = "INSERT INTO ".$tablename." ".$fieldsinsert."VALUES".$fieldsInsertvalues;
mysqli_query($conn,$sql1);
}
fclose($h);
//unlink($filepath);
}
Show me some code which will help me to insert my all rows from csv data or give me some idea that is it possible to insert the csv data in 500-500 packet.