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?

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.