Take the 2-minute tour ×

i imported the csv file data and stored in php mysql succefully without header, when i include header in csv,it takes it as data , not headers, i want to add check when any user import csv file,the script must check header fields must be same as mysql table,otherwise it exit or stop further processing,i just validates the header fields that must be exist same as in my db table, thanks in advance, the script is given below.

$file_handle = fopen("myfile.csv","r")or die("can't open file");

while (($data = fgetcsv($file_handle, 1024, ",")) !== FALSE)


$import="INSERT into customer(a_code,c_name,first_name,cell,email,phone,date,group_name) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]')";

   mysql_query($import) or die(mysql_error());*/

share|improve this question

1 Answer 1

you can mysql_query('DESC customer') to get a list of columns in the table, then compare it to your CSV header to see if they match

Also try array_combine($header_line_array, $current_line_array) to get a keyed array, then you can allow your CSV columns to be in a different order then the DB columns

share|improve this answer
can you implement this according to my senario i will be thankfull to you for this –  user1639357 Aug 31 '12 at 17:54

Your Answer


