CSV + single quotes + read data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lordrt
Forum Commoner
Posts: 34
Joined: Sun Jul 19, 2009 11:44 pm

CSV + single quotes + read data

Post 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();
?>
 
etherkye
Forum Newbie
Posts: 21
Joined: Fri Aug 07, 2009 3:40 am

Re: CSV + single quotes + read data

Post by etherkye »

Will

Code: Select all

$data  = str_replace(''','',$data );
Not remove the ' for you?
lordrt
Forum Commoner
Posts: 34
Joined: Sun Jul 19, 2009 11:44 pm

Re: CSV + single quotes + read data

Post 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
etherkye
Forum Newbie
Posts: 21
Joined: Fri Aug 07, 2009 3:40 am

Re: CSV + single quotes + read data

Post by etherkye »

Code: Select all

$data = fgetcsv($fd,0,''');
?
lordrt
Forum Commoner
Posts: 34
Joined: Sun Jul 19, 2009 11:44 pm

Re: CSV + single quotes + read data

Post 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
Post Reply