Solved

How do I add a header row to this txt file export

Posted on 2015-02-13
2
88 Views
Last Modified: 2015-02-13
This script works perfectly, however, I need to add a header row to the txt output file.  How and where do I do that?

$fh = fopen('ShoppingFeeds/Bing/Bing.txt', 'w');

$sql = 'SELECT * FROM Feeds_BD_Bing';
$pdos = $conn->prepare($sql);

// TRY THE QUERY
try
{
    $pdos->execute();
}
catch(PDOException $exc)
{
    var_dump($exc);
    trigger_error($exc->getMessage(), E_USER_ERROR);
}

// DETERMINE HOW MANY ROWS OF RESULTS WE GOT
$num     = $pdos->rowCount();
$num_fmt = number_format($num);
if (!$num)
{
    echo "<br/>QUERY: $sql ";
    echo "<br/>FOUND NO DATA ";
    echo PHP_EOL;
}
else
{
    echo "<br/>QUERY: $sql ";
    echo "<br/>FOUND $num_fmt ROWS OF DATA ";
    echo PHP_EOL;
}

//ADD HEADER ROW HERE???   PHP echo???

// ITERATE OVER THE RESULTS SET TO SHOW WHAT WE FOUND
while ($row40 = $pdos->fetch(PDO::FETCH_ASSOC))
{
    
	
	foreach ($row40 as $item) {
        fwrite($fh, $item);
        if ($item != $last)
            fwrite($fh, "\t");
    }
    fwrite($fh, "\n");
	
	
	
	
	
}

Open in new window

0
Comment
Question by:lawrence_dev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 40608003
Before this line:
// ITERATE OVER THE RESULTS SET TO SHOW WHAT WE FOUND
while ($row40 = $pdos->fetch(PDO::FETCH_ASSOC))
{

Open in new window


...add:
fwrite($fh, "ColumnA,ColumnB,ColumnC...etc...\n");

Open in new window

0
 

Author Comment

by:lawrence_dev
ID: 40608776
Thanks Gr8Gonzo!  Really appreciate the help!  Worked great!
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question