Extracting data from a MySQL database and displaying it in a table on the screen is all very good, but what about if you want to use that data in, say, Microsoft Excel? The answer is to export the data in comma separated value (csv) format, which can then be opened in any spreadsheet package.
This sample PHP script shows a very simple way of doing this. It's maybe not the most elegant way of solving the problem, and there are certainly plenty of more substantial solutions available on the Web, but it works, and that's the main thing!
In the following PHP script I've used sample data, so you'll need to change these so that they are appropriate for your web server and database environment.
<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"bankcharges.csv\"");
mysql_connect("localhost", "root", "abc123");
$result = @mysql_query("select month,bank_charges from database_name.database_table");
while ($row = @mysql_fetch_assoc($result)){
$month = $row['month'];
$bank_charges = $row['bank_charges'];
$csv .= "{$month},{$bank_charges}\n";
}
echo $csv;
mysql_close();
?>
Go back to MySQL Tutorials home page
Go back to Tutorials home page
Earnings Tracker is John Dixon Technology's FREE open source accounting and bookkeeping software tool.
The software is written in PHP and MySQL and is available to use for FREE online, or as a FREE download.
Earnings Tracker is aimed at contractors and freelancers, and lets you record invoice amounts, salaries, income tax, pension contributions, employers and employees national insurance contributions, and calculates the amount of VAT and corporation /business tax due, and the size of dividends that can be taken by shareholders.
Earnings Tracker can also be used simply as a dividend, corporation tax, or VAT calculator.
|
|
|
|
|