SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict Phil-man's Avatar
    Join Date
    Nov 2000
    Posts
    291
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi. I'm trying to import a text file into MySQL. I can't use the MySQL function to do it directly - I don't have access. The web host offers to do it for me, but I don't want to bug them every time I need to do an import, especially when I'm testing. (That's just a little bit of background).

    So, I'm using PHP, and I've got it working just fine, sort of. The text file has the fields separated by commas and enclosed in quotes. I simply "explode" each line at the commas, then remove all quotes, and I'm good to go. However, as luck would have it, some (not a lot) of the fields for some records have a comma in them, so when I do a PHP "explode" on the commas, I've got an obvious problem. Worse, the text file is coming from a really lame proprietary database that doesn't allow the flexibility to specify a delimiter such as the pipe (|) character. It's comma-delimited and that's it.

    I thought of maybe doing the explode on '"," (that's a double quote followed by a comma) since it's highly unlikely that a comma would be the first character of any particular field. That way, it would ignore the commas in the middle of fields. However, that causes me to have to remove the quotes AFTER I do the explode, and when I try to do that, it doesn't work at all. Here's some code:

    $file = file("test_import1.txt");
    foreach($file as $line) {
    $sql .= "INSERT INTO MailList SET ";
    $line = ereg_replace('"',"",$line);
    $data = explode('",',$line);
    etc...

    The above works fine. But the following does not:

    $file = file("test_import1.txt");
    foreach($file as $line) {
    $sql .= "INSERT INTO MailList SET ";
    $data = explode('",',$line);
    $data = ereg_replace('"',"",$data);
    etc...

    I guess it doesn't like me trying to use that function on an array like that. :-)

    Any ideas? Thanks!

  2. #2
    SitePoint Zealot moshe_be's Avatar
    Join Date
    Dec 2000
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not use phpMyAdmin? since you seem to got MySQL and PHP support just not telnet access.

    You can get it at: http://www.hotscripts.com

  3. #3
    SitePoint Addict Phil-man's Avatar
    Join Date
    Nov 2000
    Posts
    291
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't I still have the same problem with commas contained in the actual fields?

  4. #4
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ask your host if they have phpMyAdmin installed already, or you could do it, pretty simple.
    Location for phpMyAdmin: www.phpwizard.net
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  5. #5
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $file = file("test_import1.txt");
    foreach($file as $line) {
    $sql .= "INSERT INTO MailList SET ";
    $data = explode('",',$line);
    $data = ereg_replace('"',"",$data);

    There are some problem with this piece of code:
    $data is an array while ereg_replace expect all 3 strings as its args.
    also, for explode, the second arg should be the long string that you want to break it up and the 3rd arg is the limit (integer value)
    Check php.net for documentation of each function.
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •