0

In my code, I need to preserve all commas in each fields from the mysql table in CSV Export...

For this I used the double quotes to all fields. This code is perfectly running in the Linux System.. While running the same code in windows, double quotes also displaying in Exported CSV file.. What is the problem in this? can anyone tell me why it is happening like this?

    function  arrayToCSV($array, $header_row = true, $col_sep = ",", $row_sep = "\n", $qut = '"',$pgeTyp ="")
    {
    /*col_sep =htmlentities($col_sep);
    $row_sep =htmlentities($row_sep);
    $qut =htmlentities($qut);*/
    if (!is_array($array) or !is_array($array[0])) return false;
    //Header row.
    if ($header_row)
    {
    foreach ($array[0] as $key => $val)
    {
    //Escaping quotes.
    $key = str_replace($qut, "$qut$qut", $key);
    $output .= $col_sep.$qut.$key.$qut;
    }
    $output = substr($output, 1)."\n".$row_sep;
    }
    //Data rows.
    foreach ($array as $key => $val)
    {
    $tmp = '';

    foreach ($val as $cell_key => $cell_val)
    {  
    if($pgeTyp!="twitter" && $pagTyp!="jigsaw" && $pgeTyp=="")
    $timeValue = @checkTimeStamp($cell_val);
    else  $timeValue = '';
    if($timeValue=="True")
    {
    $cell_val = str_replace($qut, "$qut$qut", convert_time_zone($cell_val,'d-M-y h:i:s'));
    }
    else
    {
    $cell_val = str_replace($qut, "$qut$qut", $cell_val);
    }

    $tmp .= $col_sep.$qut.$cell_val.$qut;
    }
    $output .= substr($tmp, 1).$row_sep;
    }
    return $output;
    }

Function call is,

$dbResult1[] =array('url_twitter_userid' => $selecttwittermainFtch['url_twitter_userid'],'url_tweet_name' => "$url_tweet_name",'url_twitter_uname' => "$url_twitter_uname",'url_twitter_url' => $selecttwittermainFtch['url_twitter_url'],'url_twitter_location' => "$url_twitter_location",'url_twitter_followers' => $selecttwittermainFtch['url_twitter_followers'],'url_twitter_following' => $selecttwittermainFtch['url_twitter_following'],'url_modified_on' => $modifiedon);   
echo arrayToCSV($dbResult1, true, ", ", "\n",  '','twitter');                 
3
  • Please show your code. Commented Apr 4, 2013 at 12:28
  • Could you please post youe php code? Commented Apr 4, 2013 at 12:30
  • Use same editor to view your file. You are adding quotes so it will be there. Commented Apr 4, 2013 at 12:42

2 Answers 2

1

Try this:

function sqlQueryToCSV($filename, $query)
{
    $result = mysql_query($query);
    $num_fields = mysql_num_fields($result);
    $headers = array();
    for ($i = 0; $i < $num_fields; $i++) {
            $headers[] = mysql_field_name($result , $i);
    }
    $fp = fopen(mysql_real_escape_string(getcwd().'\\'.$filename), 'w');
    if ($fp) {
 fputcsv($fp, $headers);
 while ($row = mysql_fetch_array($result)) {
     $arrayValues = array();
     foreach ($headers as $header)
     {
         $arrayValues[] = $row[$header];
     }
     fputcsv($fp, $arrayValues);
 }
    }
    fclose($fp);
}

Source: http://forums.exchangecore.com/topic/907-function-to-convert-mysql-query-to-csv-file-with-php/

Sign up to request clarification or add additional context in comments.

1 Comment

this is applicable for the export of values from a file.but i need to export directly from mysql database not from file
0

When you are opening it in Windows with whatever program (Guessing Excel), you should be able to specify what characters you use to delimit/surround the fields. It's probably just assuming that you are just wanting to use commas to separate but ignoring the double quotes surrounding them.

1 Comment

If I remove this Double quotes then for each commas in a field , it splits the data into next cell.. How can I avoid this cell splitting

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.