1

I want to try using LOAD_DATA_INFILE, as I have search, its the easiest and fastest way to import csv data to my database table. I just have some questions I do hope anyone could tell me how.

I have files that were uploaded using an android device as my client. Now, I don't know the filenames, I just know that their file extension is ".csv". These are my issues:

I am trying to get the filenames using the glob() function, problem is, I also need the filename as it is one of the fields in my table. How can I do that? And with this case, is it possible for me to still use the LOAD_DATA_INFILE?

My current code can insert data from the csv file to my db table, but I want another workaround which is the load_data_infile. And I also cannot get the filename to be inserted.

$dir = "CsvFiles/";
$currentFile = glob($dir."*.csv");
foreach ( $currentFile as $filename) {
    $handle = fopen($filename,"r");
    }

    //loop through the csv file and insert into database 
    do { 
        $file = basename ($filename,".csv");
        if ($data[0]) { 
            mysql_query("INSERT INTO tblCoordinates(filename,lat,lon,dtime,cmt,cnt,ea,area,bsn,hsn,husn,name) VALUES 
                ( 
                    '$file', //this should be the filename, I still cannot get it
                    '".addslashes($data[0])."',
                    '".addslashes($data[1])."',
                    '".addslashes($data[2])."',
                    '".addslashes($data[3])."',
                    '".addslashes($data[4])."',
                    '".addslashes($data[5])."',
                    '".addslashes($data[6])."',
                    '".addslashes($data[7])."',
                    '".addslashes($data[8])."',
                    '".addslashes($data[9])."',
                    '".addslashes($data[10])."'
                ) 
            "); 
        }  
    } while ($data = fgetcsv($handle,1000,",","'")); 

Note: I am still using mysql. my boss told me to use that instead of mysqli.

user3201441
  • 167
  • 1
  • 12
  • you should use `mysql_real_escape_string()` instead of addSlashes, addSlashes doesn't account for hexidecimal quotes. Also I suggest searing for your problem before asking about it ~ http://stackoverflow.com/questions/18915104/php-import-csv-file-to-mysql-database-using-load-data-infile and http://stackoverflow.com/questions/14366928/how-to-return-just-file-name-using-glob-in-php – ArtisticPhoenix Jul 29 '15 at 06:39
  • As for putting the filename in the CSV file, simple open it in excel and add it. And I think loading it through MySql and the cli http://stackoverflow.com/questions/5152921/import-sql-file-from-mysql-console is faster. – ArtisticPhoenix Jul 29 '15 at 06:46

0 Answers0