Solved

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

Posted on 2015-02-13
2
72 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
2 Comments
 
LVL 34

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
calculated column 12 74
Why isnt it sending mail from my php but is from my server 10 33
Update database from a form 8 35
Store image url and display it for user profile 6 23
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now