Jump to content

Recommended Posts

I'm creating a CSV file using php and what to extract data from mysql however each record needs to be on a new line and the current coding isn't doing this. Any suggestions?

 

 

<?php
include "common/db_connect.php";
$date = date("Y-m-d");
$rand = rand(10000000000,999999999999999999);
$query=mysql_query("SELECT * FROM `DPD` WHERE `date`='$date' AND `submitted`='0'");
$content="";
 
  while($fetch_DPD=mysql_fetch_object($query)){
 
  $content .= "".$fetch_DPD->ref.",".$fetch_DPD->date.",".$fetch_DPD->name.",".$fetch_DPD->add1.",".$fetch_DPD->add3.",".$fetch_DPD->add4.",".$fetch_DPD->post_code.",".$fetch_DPD->country_code.",".$fetch_DPD->service.",".$fetch_DPD->no_of_parcels.",".$fetch_DPD->weight.", ".$fetch_DPD->tele.", ".$fetch_DPD->SMS.", ".$fetch_DPD->email.", ".$fetch_DPD->value.", ".$fetch_DPD->description."" . PHP_EOL;
 
     $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/admin/temp/".$rand.".csv","wb");
 fwrite($fp,$content);
  fclose($fp);
  }
 
?>
Link to comment
https://forums.phpfreaks.com/topic/284843-php-eol-not-working/
Share on other sites

PHP_EOL is defined for the system the script is run on. So if you run it on a linux system, it will be defined as '\n'. If you run it on Windows, it will be defined on '\r\n'. So if you run it on a linux system and then open it as-is on a windows system, you aren't going to see the line breaks.
Link to comment
https://forums.phpfreaks.com/topic/284843-php-eol-not-working/#findComment-1462692
Share on other sites

also FYI, php does have csv handling functions fgetcsv for reading and fputcsv for writing. You should use these instead of fwrite since they account for and escape embedded delimiters (quotes and commas).

 

Also want to mention that I'm not sure your logic is correct.. you have a .= operator for your $content so it's appending the previous row to the new row and then your file handling is also in your while loop so you're basically creating an aggregated list like this:

 

a

ab

abc

abcd

abcde

 

Actually with the newlines, it'd look more like this:

 

a

a

b

a

b

c

a

b

c

d

a

b

c

d

e

 

I doubt that's what you want.. so you should either change that .= to = or else move your file handling outside of the loop.

Link to comment
https://forums.phpfreaks.com/topic/284843-php-eol-not-working/#findComment-1462695
Share on other sites

Ah thank you for your response that's interesting because it running on linux however i'm downloading it onto my windows laptop so it would clearly change in notepad too what my sever would read it as.

 

Is that correct?

right, since it's run on linux, but you're opening the file in windows notepad, well windows notepad won't recognize the linebreaks. Most "real" code editors like html-kit, etc. will automatically convert it for you so that you see it the way it should. Also, certain programs like excel are smart enough to make the conversion. But I know for a fact that regular notepad isn't smart enough to do it.

 

The safer thing to do would just be use "\r\n" (make sure it's in double quotes so php will treat it as escape chars) instead of PHP_EOL. Linux will ignore the \r and windows will read it properly. This will mess with some other systems for example older macs but you can probably not care about that.

Link to comment
https://forums.phpfreaks.com/topic/284843-php-eol-not-working/#findComment-1462697
Share on other sites

actually i lied about the stacking data. I noticed you're opening the file in "wb" mode. the "w" overwrites the entire file each time you open it so you're effectively getting what you want on the last iteration of the loop. However, that's not very efficient. You should still change the .= to = and use "a" instead of "w". This will open the file and put the pointer at the end of the file and append to it. That way you don't use extra memory holding a stacking $content value.

Link to comment
https://forums.phpfreaks.com/topic/284843-php-eol-not-working/#findComment-1462699
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.