PHP Classes

File: example_mysql_to_csv.php

Recommend this page to a friend!
  Classes of Thiemo Kreuz   TM::MyCSV   example_mysql_to_csv.php   Download  
File: example_mysql_to_csv.php
Role: Example script
Content type: text/plain
Description: How to dump a MySQL table into CSV
Class: TM::MyCSV
Manage CSV files like database tables
Author: By
Last change: Accessible without user login
Date: 14 years ago
Size: 1,054 bytes


Class file image Download

 * This example script for the TM::MyCSV class shows how easy it is to put the
 * result from a MySQL query into a CSV file. The extended class
 * MyCSV_MySQL.class.php converts data in the other direction and is not used in
 * this example.
 * @author Thiemo Mättig (


$csv = new MyCSV();

// Change the delimiter to ";" or "\t" if needed.
$csv->delimiter = ",";

// MySQL host, login name, password and database name.
mysql_connect("localhost", "root", "");

// The SQL query can contain all combinations of WHERE, ORDER BY and so on.
$sql = "SELECT * FROM `test`";
$result = mysql_query($sql);

// Push all data into the MyCSV object.
while ($record = mysql_fetch_assoc($result))

// If the delimiter is "\t", the content type should be
// "text/tab-separated-values" or "text/plain".
header("Content-Type: text/comma-separated-values");

// Dump the CSV data to screen.