Page 1 of 1

CSV + single quotes + read data

Posted: Fri Aug 07, 2009 1:10 am
by lordrt
The following data is being inserted to a mysql DB

195, 'Pain Surprise vide P', '<p>Pain Surprise vide P</p>',14, ' ', ' ', 0

I am testing using a .txt file and while the single quotes were present, the data was not inserted properly to my tables, but after manually removing them to

195, Pain Surprise vide P, <p>Pain Surprise vide P</p>,14, , , 0 the data was inserted correctly.

The code I was using in the text file was as follows:

Code: Select all

 
<?php
 
//mysql_connect("127.0.0.1", "root", "") or die (mysql_error());
//mysql_select_db("drupal_db") or die (mysql_error());
//echo "connected to db" ."<br/>";
 
include("import_article.php");
 
$path = "sites/path/files/";
$file = "*.txt";
 
foreach (glob($path.$file) as $filename)
{
    $fd = fopen($filename, "r");
    while (!feof($fd))
    {
        $data = fgets($fd);
        $delimiter = ",";
        $content = explode($delimiter, $data);
        //echo 'content:' .$content[0].$content[1].$content[2].$content[3].$content[4].$content[5].$content[6] ."<br/>";
        ImportArticle($content[0], $content[1], $content[2], $content[3], $content[4], $content[5], $content[6]);
        
    }
    echo "read complete" . "<br/>";
    fclose($fd);
}
//echo "close conn";
//mysql_close();
?>
 
I now however have to work with the same data which will now be contained in a CSV file, and I want to know if by using fgetcsv it will parse the single quotes and commas or I have to remove the single quotes manually?? CSV code as follows:

Code: Select all

 
<?php
 
// import php file: import_article.php
require("import_article.php");
 
 
// mysql_connect("127.0.0.1", "root", "") or die (mysql_error());
// mysql_select_db("drupal_db") or die (mysql_error());
 
// read first .csv file encountered
 
$path = "sites/path/files/";
$sfile = "*.csv";
 
foreach (glob($path.$sfile) as $filename){
    echo $filename . "<br/>";
    $file = fopen($filename, 'r');
    while (($data = fgetcsv($file, ",")) !== False)
    {
        //echo $data[0]. $data[1]. $data[2]. $data[3]. $data[4]. $data[5]. $data[6]. "<br/>";
        ImportArticle($fdta[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6]);
    }
    echo "read complete";
    fclose($file);
    // unlink ($filename);
}
// echo "closing connection";
// mysql_close();
?>
 

Re: CSV + single quotes + read data

Posted: Fri Aug 07, 2009 3:51 am
by etherkye
Will

Code: Select all

$data  = str_replace(''','',$data );
Not remove the ' for you?

Re: CSV + single quotes + read data

Posted: Fri Aug 07, 2009 6:02 am
by lordrt
etherkye wrote:Will

Code: Select all

$data  = str_replace(''','',$data );
Not remove the ' for you?

Tried it, sometimes it work but sometimes it retrieves the data along with the quotes, which then causes error when insert into tables

Re: CSV + single quotes + read data

Posted: Fri Aug 07, 2009 6:25 am
by etherkye

Code: Select all

$data = fgetcsv($fd,0,''');
?

Re: CSV + single quotes + read data

Posted: Fri Aug 07, 2009 7:04 am
by lordrt
thx for the help but manage to make it work:

Code: Select all

 
while (($data = fgetcsv($file, 0, ",", "'")) !== False)
 
had to add the length part, which is 0 in my case to not limit number of chars read