Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 98,767 PHP Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 1,043 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a PHP Expert

Register to Make This Box Go Away!


Writing to CSV file

 
Reply to this topicStart new topic

Writing to CSV file

musya
post 6 May, 2008 - 10:00 AM
Post #1


D.I.C Head

Group Icon
Joined: 25 Apr, 2007
Posts: 213



Dream Kudos: 50
My Contributions


Okay im trying to write the contents of a sql result to a csv file and each row in the db is a new row in the file, it all works but the only problem is that the newest row overwrites the previous one, so at the end i get the last row from the db and thats it.
here is my code, i hope that makes sence? i should have 100 or so values but i only get 1 wich is the last one is in the row, and it all echo correctly on the screen just not to the file, im overwritting the previous row and not creating a new row.

CODE

        while ($i = mysql_fetch_assoc($result))
        {
            $userID = $i['userID'];
            $firstName = $i['firstName'];
            $lastName = $i['lastName'];
            $emailAddress = $i['emailAddress'];
            $agentId = $i['agentId'];
            $signUpDate = $i['signUpDate'];
            $content = $userID.','.$firstName.','.$lastName.','.$emailAddress.','.$agentId.','.$signUpDate.'\n';
            echo $content.'<br>';
            $FileName = "cmic_report/RegularReport.csv";
            $FileHandle = fopen($FileName, 'w') or die("can't open file");
            fwrite($FileHandle, $content);
        }
        fclose($FileHandle);


any help would be great, thanks
User is offlineProfile CardPM

Go to the top of the page


musya
post 6 May, 2008 - 10:25 AM
Post #2


D.I.C Head

Group Icon
Joined: 25 Apr, 2007
Posts: 213



Dream Kudos: 50
My Contributions


Never mind i got it, it suppose to be a+ instead w. Thanks anyways ill see if i get stuck again.
User is offlineProfile CardPM

Go to the top of the page

deepamanohar
post 7 May, 2008 - 06:15 AM
Post #3


New D.I.C Head

Group Icon
Joined: 14 Mar, 2008
Posts: 9



Dream Kudos: 150
My Contributions


hi

I have a small suggestion, i am able to see that you are trying to write the file each and every time untill the while loop fails because of this your program may slow down the process of execution.

so you need to just append your $content = as $content.= and execute your file writing function once by placing it after the while loop so your code will be something like this

CODE

while ($i = mysql_fetch_assoc($result))
        {
            $userID = $i['userID'];
            $firstName = $i['firstName'];
            $lastName = $i['lastName'];
            $emailAddress = $i['emailAddress'];
            $agentId = $i['agentId'];
            $signUpDate = $i['signUpDate'];
            $content.= $userID.','.$firstName.','.$lastName.','.$emailAddress.','.$agentId.','.$signUpDate.'\n';
            
          
        }
         echo $content.'<br>';
          $FileName = "cmic_report/RegularReport.csv";
            $FileHandle = fopen($FileName, 'a+') or die("can't open file");
            fwrite($FileHandle, $content);
        fclose($FileHandle);


User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 7/20/08 03:36PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->