PHP – Import CSV file to mysql database Using LOAD DATA INFILE

If you’d do echo($sql); before you execute it you’d see that syntax of your query is incorrect for following reasons: Filename should be enclosed in quotes rather than backticks because it’s a string literal not an identifier. There is absolutely no need to call mysql_escape_string() to specify a delimiter in FIELDS TERMINATED BY and ENCLOSED … Read more

Easy way to export a SQL table without access to the server or phpMyADMIN

You could use SQL for this: $file=”backups/mytable.sql”; $result = mysql_query(“SELECT * INTO OUTFILE ‘$file’ FROM `##table##`”); Then just point a browser or FTP client at the directory/file (backups/mytable.sql). This is also a nice way to do incremental backups, given the filename a timestamp for example. To get it back in to your DataBase from that … Read more

MySQL LOAD DATA INFILE with ON DUPLICATE KEY UPDATE

These steps can be used to emulate this functionality: Create a new temporary table. CREATE TEMPORARY TABLE temporary_table LIKE target_table; Optionally, drop all indices from the temporary table to speed things up. SHOW INDEX FROM temporary_table; DROP INDEX `PRIMARY` ON temporary_table; DROP INDEX `some_other_index` ON temporary_table; Load the CSV into the temporary table LOAD DATA … Read more

MySQL load NULL values from CSV data

This will do what you want. It reads the fourth field into a local variable, and then sets the actual field value to NULL, if the local variable ends up containing an empty string: LOAD DATA INFILE ‘/tmp/testdata.txt’ INTO TABLE moo FIELDS TERMINATED BY “,” LINES TERMINATED BY “\n” (one, two, three, @vfour, five) SET … Read more