do you want to populate your database only one time ?
Main Topics
Browse All TopicsHi, I have a CSV file that looks like the following...
TVWB09285~28 Dec 2007 13:57~DARRELL LITCHFIELD~THE JNP PARTNERSHIP~Address Line 1~-~HIGH WYCOMBE~Mainland UK~-~HP13 6LJ~DARRELL LITCHFIELD~01234567890~-~-
TVWB09283~28 Dec 2007 13:16~Barbara Thomas~C P M~Address Line 1~Warrington~Mainland UK~-~WA4 6QS~Barbara Thomas~01234567890~C P M~-~3~0~barbara.thomas@xxx
TVWB09282~28 Dec 2007 13:14~m gunn~~Address Line 1~-~Forres~Mainland UK~-~IV36 2US~m gunn~01234567890~-~-~1~0~b
TVWB09281~28 Dec 2007 13:15~Mr B Andrews~~Address Line 1~-~Orpington~Mainland UK~-~BR6 8PD~Mr B Andrews~01234567890~-~-~1~
TVWB09280~28 Dec 2007 13:12~Mr Oliver~Cassis~Address Line 1~-~Broadstairs~Mainland UK~-~CT10 3QY~Mr Oliver~01234567890~Cassis~
I need to enter this data into a MySQL database in a single fell swoop. On the above data I have separated each separate "row" the a line space. Obviously, in all realism, there won't be a line space within the CSV. However, if required, I am able to add a special character before the order number (TWVB0XXXX) in order to make this clarification possible.
So I have created my database with the following fields, which the above data needs to be inserted into, in the order specified...
ref_id
order_number
date
name
company_name
address1
address2
towncity
county
na1
postcode
deliveryname
telephone
companybackup
na2
items
na3
email
na4
bagsize
weight
deliveryinstructions
na5
Can anyone help me do this. I am good at basic level PHP, but when it gets to doing functions within functions, which this appears to be, this is where I get awful confused and mixed up.
I am assuming (maybe incorrectly) I have to explode the above data, into each row required to be added - and then I need to explode each row with the ~ charatcer in order to insert it into the DB.
I just dont know how to do it! Any help appreciated :)
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
SQLyog provides an easy way to upload cvs files. Download the community edition :
http://www.webyog.com/en/d
ok daily.
Do you erase all the content or just replace records which has been modified ? if you erase be careful you could loose some id.
Otherwise have a look at :
http://dev.mysql.com/doc/r
Note to self: Remember to not press CTRL-S in QuickEE..
When the size of the file is less than the amount php is allowed to use, then you could possibly go with:
$parts = explode("~", file_get_contents("mycsvfi
$numberOfElementsPerRow = 8;
while(count($parts) > $numberOfElementsPerRow) {
$record = array_slice($parts,0,$numb
$record = array_map($record, "mysql_real_escape_string"
$parts = array_slice($parts, $numberOfElementsPerRow);
$query = 'INSERT INTO '.$table
.' (field1,field1,field3,fiel
.' VALUES("'
implode('","', $record)
.'")'
echo '<pre>Query: ' . $query . '</pre>';
}
Kind regards
-r-
Roonaan, this is very close to working i feel, I have the following on my page where $parts is the content of the file... Do I need to be changing "mysql_real_escape_string"
ConnDB contains my Connection string to the DB. I am currently getting a blank screen when loading the info with no MySQL errors, so I am struggling to see what is wrong.
---
$parts = $contents;
$numberOfElementsPerRow = 23;
while(count($parts) > $numberOfElementsPerRow) {
$record = array_slice($parts,0,$numb
$record = array_map($record, "mysql_real_escape_string"
$parts = array_slice($parts, $numberOfElementsPerRow);
mysql_select_db($database_
$query = 'INSERT INTO bp_export (ref_id, order_number, date, name, company_name, address1, address2, towncity, county, na1, postcode, deliveryname, telephone, companybackup, na2, items, na3, email, na4,
bagsize, weight, deliveryinstructions, na5) VALUES ("' . implode('","', $record). '")';
$Result = mysql_query($query, $connDB) or die(mysql_error());
echo '<pre>Query: ' . $Result . '</pre>';
}
$content is the CSV file - so not an array just the content itself.
In full (minus my connection details) my file is as follows...
$contents = file_get_contents("bpdata.
$parts = $contents;
$parts = explode("~", $parts);
$numberOfElementsPerRow = 23;
while(count($parts) > $numberOfElementsPerRow) {
$record = array_slice($parts,0,$numb
$record = array_map($record, "mysql_real_escape_string"
$parts = array_slice($parts, $numberOfElementsPerRow);
mysql_select_db($database_
$query = 'INSERT INTO bp_export (ref_id, order_number, date, name, company_name, address1, address2, towncity, county, na1, postcode, deliveryname, telephone, companybackup, na2, items, na3, email, na4,
bagsize, weight, deliveryinstructions, na5) VALUES ("' . implode('","', $record). '")';
$Result = mysql_query($query, $connDB) or die(mysql_error());
echo '<pre>Query: ' . $Result . '</pre>';
}
With the above I get the following errors but I do not understand these :S
Warning: array_map() [function.array-map]: The first argument, 'Array', should be either NULL or a valid callback in /usr/local/www/vhosts/tv-w
Warning: implode() [function.implode]: Bad arguments. in /usr/local/www/vhosts/tv-w
Column count doesn't match value count at row 1
Any ideas?
Try replacing
$record = array_map($record, "mysql_real_escape_string"
with
$record = array_map("mysql_real_esca
This call applies mysql_real_escape_string to all elements in the record, making sure your db is safe from sql injection. If it still not works you can outcomment the line while debugging.
Business Accounts
Answer for Membership
by: RoonaanPosted on 2007-12-31 at 04:55:27ID: 20554394
SamDavis,
What size is your file?
Kind regards,
-r-