Jump to content

Recommended Posts

or at least just echo out the query ( insert statement). I wrote some code to take a very simple spreadsheet with name and address and import into a database table. Simple I thought, right? Well does anyone have some snippet of code that addresses apostrophes in names, extra commas and so on in the data?

 

To keep it simple, I will just use first and last name. tab delimited. Does anyone have any code that is more sophsticated? Thanks Kip

 

<code>

$file = "philly.csv";

 

if($handle = fopen($file, 'r')) {

 

while(!feof($handle)) {

 

  $line=fgets($handle);

 

  $cells = explode("\t", $line);

 

echo " insert into wp_joinus (fname, lname) values ('{$cells[1]}' , '{$cells[2]}') ";

 

  }

fclose($handle);

}

 

</code>

Thank you guys.  ;D But, I my problem is not the type of csv. Tab delimited is always preferred and the outcome is to generate the insert statements. As in my near last line of the code is:

 

<code>

echo " insert into wp_joinus (fname, lname) values ('{$cells[1]}' , '{$cells[2]}') ";

</code>

 

Since this has got to be a very common procedure, I would think there is some snippet to handle this. I use trim and a snippet I call msqlprep which takes care of magic quotes on or off depending on the version of php and so on. So, I was looking for some snippet that generates insert sql statements and does a really good job of cleaning or validating the values. (don't have to worry about the mysql connection and like that, just echo out the inserts.

 

In response to dabaR, I always go php.net first. Which section do you mean? Your statement: "

 

I think you will be happy to hear about http://ca3.php.net/manual/en/function.fgetcsv.php did you mean?

 

I don't see which snippet that generates the insert statements. Please define.

 

Thanks for all your help guys. But, if you don't know, then it looks I have the really good snippet. I would be happy to share.

 

Kip

 

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.