Jump to content

[SOLVED] PHP reading file incorrectly?


_tina_

Recommended Posts

I am reading a comma delimited file into php to insert into a MySql database as follows:

 

<?
  $fcontents = file ('file.csv'); 

  for($i=0; $i<sizeof($fcontents); $i++) { 
      $line = trim($fcontents[$i]); 
      $arr = explode(",", $line); 
     
      $sql = "insert into TABLENAME values ('". 
                  implode("','", $arr) ."')"; 
      mysql_query($sql);
      echo $sql ."<br>\n";
      if(mysql_error()) {
         echo mysql_error() ."<br>\n";
      } 
}
?>

 

Everything is acting as expected.  The only problem is that it seems to be looping on extra time and giving me a blank set of results at the end of each line and producing the following:

 

insert into TABLENAME values ('"name"','"random data"','"random data"','"random data",'','','','')

 

Do you know what might be the problem?

Thanks in advance :)

Link to comment
https://forums.phpfreaks.com/topic/158263-solved-php-reading-file-incorrectly/
Share on other sites

A CSV's data it like this

"MadTechie","is great"

and your doing this

$arr = explode(",", $line); 

which Breaks it up into "MadTechie" and "is great" with the quote,

This would remove the quotes

$arr = explode('","', $line); 

 

But you should really use fgetcsv(),

 

however, i don't see the point of using

$arr = explode(",", $line); 

then

implode("','", $arr) ."')"; 

 

when you could just do

$sql = "insert into TABLENAME values ($line)"; 

I assume your delimiter or enclosure was incorrect

it should be like this

fgetcsv($handle, 1000, ",", '"')

 

could you provide some sample data from the CSV file and show what you wish to keep/loss

ie

"Hello","Blar","Welcome","Yoyo",554,"Lollypop",123

"More","coffee","GoodBye","xbox",45,"Candy",456

only remove the red

 

could you provide some sample data from the CSV file and show what you wish to keep/loss

ie

"Hello","Blar","Welcome","Yoyo",554,"Lollypop",123

"More","coffee","GoodBye","xbox",45,"Candy",456

only remove the red

 

 

This is what is one line from the CSV:

 

"john", "E-Commerce Integration", "Notes notes",,,,

 

I basically just want to take each field out, remove slashes, commas and just grab the text and insert into a database one row at a time.  The extra commas at the end of each row are my problem.  I am doing something like:

        $lineseparator = "\n";
        $linearray = explode($fieldseparator,$line);
        $linemysql = implode("','",$linearray);

        $query = "insert into $databasetable ($fields) values('$linemysql');";

 

This is giving me the query correctly except it is adding quoted commas to the end as in:

 

insert into TABLENAME values ('"name','data','data',,,,, ')

 

Thanks.

<?php
$line = '"john", "E-Commerce Integration", "Notes notes",,,,';

$line = trim($line,',"'); //remove , and "
//it should now be 'john", "E-Commerce Integration", "Notes notes'

$linearray = explode('", "',$line); //Get and array //or below
/* $linearray should be
array(
john
E-Commerce Integration
Notes notes
)
*/

$sqlvalue = "'".implode("','",$linearray)."'"; //you need the first and last '
//$sqlvalue should be 'john','E-Commerce Integration','Notes notes'

// i'll assume $fields are okay!
$query = "insert into $databasetable ($fields) values($linemysql);";

<?php
$line = '"john", "E-Commerce Integration", "Notes notes",,,,';

$line = trim($line,',"'); //remove , and "
//it should now be 'john", "E-Commerce Integration", "Notes notes'

$linearray = explode('", "',$line); //Get and array //or below
/* $linearray should be
array(
john
E-Commerce Integration
Notes notes
)
*/

$sqlvalue = "'".implode("','",$linearray)."'"; //you need the first and last '
//$sqlvalue should be 'john','E-Commerce Integration','Notes notes'

// i'll assume $fields are okay!
$query = "insert into $databasetable ($fields) values($linemysql);";

 

Thank you very much for the help.  It i now inserting into the db.  Thanks again for the help :)

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.