PHP Mysql Upload?

Soldato
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
In phpmyadmin you can upload a full sql dump of the database/tables and add it/replace the tables with this info.

How could one achieve this independantly?

CHeers
 
I see, in that case here's an example of how to replace your existing table contents. With a few modifications you should be able to import whatever you like:

PHP:
# Delete the current content of the table
$result = mysql_db_query('$sql_id',"DELETE FROM table_name") or die ("Invalid DELETE query");

# Optimize the current table (recover empty space)
$result = mysql_db_query('$sql_id',"OPTIMIZE TABLE table_name") or die ("Invalid OPTIMIZE query");

# Load local comma separated, fields enclosed by quotes text database - File has to be in the same directory of this file
$result = mysql_db_query('$sql_id',"LOAD DATA LOCAL INFILE 'export_file_name.txt' INTO TABLE table_name FIELDS TERMINATED BY ',' ENCLOSED BY '\"'") or die ("Invalid DATA LOAD query");

# Get how many records are present in the table now
$result = mysql_db_query('$sql_id',"SELECT * from table_name") or die ("Invalid SELECT query");
$rows_count = mysql_num_rows($result);

echo "Records: $rows_count"; mysql_free_result($result);
 
As the code stands it will expect the export file to be in the same directory as the script. I don't need to have a seperate directory so not tried but you should be able to include a path as part of the filename.
 
Hmmm... I could always upload the file (or if textarea, write the file) then import it into mysql then delete it :)

EDIT - that sorts out csv/tab deliminated (with slight modification) what about importing an sql dump? i.e. to write the tables as well as importing data...
 
Last edited:
Back
Top Bottom