Jump to content

Convert raw text data into a mysql insert statement


Omzy

Recommended Posts

I've got some data which I need to populate into a database. Currently it is the following form:

 

East Kilbride = 10,

Ellesmere Port = 1,

Worksop = 6,

Worthing = 4,

Wrexham = 11,

Yeovil = 3,

York = 8,

 

(that is just a sample of the data, there are about 400 rows)

 

So it's basically like a key & value format, I can of course do a find&replace on the equals symbol to get the => operator and convert the data in to an array, but the main diffculty is to get the keys/values enclosed in single quotes.

 

The idea is to build an mysql insert statement so that I can input all the data in to one of the tables. How would I go about doing this?

OK I've tried this, the thing is that the explode() function creates an array with it's own numerical indexes and that makes it more difficult to format the data.

 

I put the data in to a string ($towns) and then did the following:

 

 
$townarray=explode(",", $towns);

$townstring=implode(",", $townarray);

$newtownarray=explode("=", $townstring);

foreach($newtownarray as $key=>$value)
{
echo $value;
echo "\n";
}

 

I think there is obviously an error in this and I can't figure out how to do it..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.