PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
$numbers = file("../Book1.csv");
foreach ($numbers as $number)
{
list($data) = explode(",", $number);
$query_insert=mysql_query("INSERT INTO members SET member_id='$data'");
}
The above script works well. But if i wanted the csv file to be dynamic, should i have to copy the file to local folder and read it from there?
what do you mean by local folder? For me "../Book1.csv" is the local folder (in WWW server). So if you change the file Book1.csv and run the script, data will be imported.
If you have access to MySQL command "LOAD DATA INFILE" a very fast way to import CSV data is:
LOAD DATA INFILE 'Book1.csv' INTO TABLE members
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n';
By the way: executing INSERT command one by one for every record is not very efficient if it concerns you, specially if you have indexes on a table you are importing to. On every INSERT MySQL rebuilds index data and when the table is big it may take some time to finish the job. More data may be inserted in one INSERT query, then index data are rebuilt once (check a manual). Of course you also need to consider scalability issues. When you import much data, an other option are these commands:
thanks once again. Is it possible to copy the contents to database without uploading the file by just showing the path. Is it necessary to copy the file?
shivam0101 wrote:thanks once again. Is it possible to copy the contents to database without uploading the file by just showing the path. Is it necessary to copy the file?
Aah, that's what you mean! Yes it is necessary, but it is done automatically after user press Submit button. Then $_FILES array contains information about uploaded file copied by a Web Server to a temporary directory. Just look at $_FILES array as xpgeek suggested and you will find path to this uploaded file which you may read and do what you want.