<?php
###########################################################
# CLASS: rs2csv
# Author: Sean S. Natoewal
# Contact: hide@address.com
# Date: June 22th 2003
# Version: 1.0
#
# This class can be used to convert a resultset which has
# been generated by PHP's built-in MySQL functions to a
# comma separated values (CSV) file.
#
# If you find this script useful or are using it for one of
# your projects please send me an email telling me a little
# about the project and how useful the script was to you.
#
# If you got any questions or comments feel free to mail me.
#
############################################################
class rs2csv {
var $_str, $_fname, $_sep, $_rs, $_link, $_ctype, $_cdisp, $_con = false;
// constructor
function rs2csv() {
$this->_str = "";
$this->set_fname("file.csv");
$this->set_ctype("text/tab-separated-values");
$this->set_cdisp("attachment");
$this->set_sep(",");
}
// function to set the filename
function set_fname($fname) {
$this->_fname = $fname;
}
// function to set the content-type
function set_ctype($ctype) {
$this->_ctype = $ctype;
}
// function to set the content-disposition
function set_cdisp($cdisp) {
$this->_cdisp = $cdisp;
}
// function to set the separator
function set_sep($sep) {
$this->_sep = $sep;
}
// function to open a connection to the MySQL server
function make_con($server, $username, $password, $database_name) {
$this->_link = mysql_connect($server, $username, $password) or $this->display_err("Could not connect : " . mysql_error());
mysql_select_db($database_name, $this->_link) or $this->display_err("Could not select database");
$this->_con = true;
}
// function to close the MySQL connection
function close_con() {
// free resultset
mysql_free_result($this->_rs);
// closing connection
if ($this->_con)
mysql_close($this->_link);
}
// function to execute the SQL-query
function exec_sql($sql, $link = "") {
if (!$this->_con)
$this->_link = $link;
if ($this->_link == "" || !$this->_con)
$this->_rs = mysql_query($sql) or $this->display_err("Query failed : " . mysql_error());
else
$this->_rs = mysql_query($sql, $this->_link) or $this->display_err("Query failed : " . mysql_error());
$this->process_rs($this->_rs);
$this->close_con();
}
// function to process the resultset ($rs)
function process_rs($rs) {
$this->_rs = $rs;
if (mysql_num_fields($this->_rs) == 0)
$this->display_err("Invalid resultset. Number of fields is 0.");
if (mysql_num_rows($this->_rs) == 0)
$this->display_err("Invalid resultset. Number of records is 0.");
for ($i=0;$i<mysql_num_fields($this->_rs);$i++) {
$this->_str .= mysql_field_name($this->_rs, $i).$this->_sep;
}
$this->_str .= "\n";
while ($line = mysql_fetch_array($this->_rs, MYSQL_ASSOC)) {
foreach ($line as $col_value) {
$this->_str .= $col_value.$this->_sep;
}
$this->_str .= "\n";
}
}
// function to display an error message
function display_err($err_msg) {
echo "Error: $err_msg";
exit;
}
// function to output the CSV file
function output_csv() {
if (strlen($this->_str) == 0)
$this->display_err("Empty file");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-Length: ".strlen($this->_str));
header("Content-type: ".$this->_ctype);
header("Content-Disposition: ".$this->_cdisp."; filename=".$this->_fname);
echo $this->_str;
exit;
}
}
?>