Jump to content

[SOLVED] PHP import CSV to MySQL - Line Delimiter?


CodeFlash

Recommended Posts

Hi, I've been working on a project that takes a TCP/IP data stream and sticks it into a txt file then I send it to the web to be imported into MySQL for a live data stream type Flash application but I've ran into a hicup. The program sending the data doesnt send it in lines, so its just one long stream and I want to break it up into multiple lines. Only problem is I can't find an easy way to do it, so I thought I would ask. I've googled and googled to no avail.

 

So far this is what I have:

  $fcontents = file ('./Results.txt'); 
  # expects the csv file to be in the same dir as this script

  for($i=0; $i<sizeof($fcontents); $i++) { 
      $line = trim($fcontents[$i]); 
      $arr = explode(";", $line);
      #if your data is comma separated
      # instead of tab separated, 
      # change the '\t' above to ',' 
     
      $sql = "INSERT INTO Result values (\"".implode("\",\"", $arr)."\")";  
      mysql_query($sql);
      echo $sql ."<br>\n";
      if(mysql_error()) {
         echo mysql_error() ."<br>\n";
      } 
}

 

Right now its a a semicolon delimited text file with a | at then end of each line. So is there anyway to easily add a line delimiter?

 

Thanks so much!

Cody

on the script that takes the stream and writes it to the file you could do this

$result = str_replace("|","\r\n",$result);

that will put the lines in your file

or on the script you posted you could say

$fcontents = file_get_contents('./Results.txt');
$fcontents = explode("|",$fcontents);

 

Scott.

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.