0

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';
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Anne
  • 3
  • 1
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Aug 01 '17 at 12:03
  • Did you empty all the database connection variables intensionally, or are those actually empty variables? – RiggsFolly Aug 01 '17 at 12:04
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Aug 01 '17 at 12:05
  • Is this really all on one line `header to start from line 1 if($count>0)` – RiggsFolly Aug 01 '17 at 12:07

0 Answers0