Solved

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

Posted on 2015-02-13
2
87 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 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