Jump to content

Read MySQL database, write out each record to Txt file?


FredFogg

Recommended Posts

I have a MySQL database with each record of a person who has registered for an event, I am displaying the information on a web page for a user, but he wants to be able to print out all the records in alphabetical order by last name, first name later on so he will have a hard copy of each person who has registered at the table when they arrive.  How can I write each record to a Txt file that he can print out later that will be formated with the record contents along with each fields definition (Ex. Last Name - Smith, First Name - John, etc)?

Link to comment
Share on other sites

I don't want him to have to hit Print for each record, there will several hundred people that have registered.  There has to be a way to read each record from the database, write the headers and the record to a txt file through a loop.  The later on he can print the txt file.  I just don't know how to do it in PHP.

Link to comment
Share on other sites

A simple example...

 

<?PHP
include('db.php'); /* change to  YOUR database connection */
$query = "SELECT * FROM main ORDER BY lastname, firstname"; /* change to YOUR table name */
$result = mysql_query($query);
$output = "Last name, First name\r\n";
while($row= mysql_fetch_array($result)){
  $output = $output . $row['lastname'] . ", " . $row['firstname'] . "\r\n";
}
$newfile="newfile.txt"; 
$file = fopen ($newfile, "w"); 
fwrite($file, $output); 
fclose ($file); 
?>
<a href="newfile.txt">Right click to view the list - left click to save the list</a>
<?PHP

?>

Link to comment
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.