I am failry new to php coding. I am trying to import data into mysql db from a csv document. I want to extract the first 3 rows so that i know what headings are in the document supplied by the end user.I have tried to do a loop and use break but it gives me the first column instead of first row so maybe transposing the array first and looping through will do it?
But i don't know how to transpose the array of the incoming csv.
echo implode(",", $emapData); break;
gives me
"id,firstname,lastname,dob,age,postcode,mobile,email"
which is what i want but i can't seem to echo each field independently (technically i want to loop through each value comma separated) as i want to use it in an html table further down in the code. Please find attached the code. I hope that makes sense and any help will be very much appreciated. Thanks very much
if(isset($_POST["Import"]))
{
//First we need to make a connection with the database
$host=''; // Host Name.
$db_user= ''; //User Name
$db_password= '';
$db= ''; // Database Name.
$conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
mysql_select_db($db) or die (mysql_error());
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0) {
$file = fopen($filename, "r");
//echo 'countis'.$count.$emapData.'firstrow'.$emapdata[0][0];
$count = 0;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
$count++;
if($count>1)
//starting point is line 2 of the excel spreadsheet given line 1 is the
header to start from line 1 if($count>0)
{
$sql = "INSERT into tablename(xx) values (yyy)";
mysql_query($sql);
}
}
fclose($file);
echo 'CSV File has been successfully Imported';
// header('Location: xxxxx.php');
} else
echo 'Invalid File:Please Upload CSV File';
}