Location: PHPKode > projects > XMasDB > xmasdb/export.php
<?php
// start the session
session_start();

// is the one accessing this page logged in or not?
if (!isset($_SESSION['basic_is_logged_in']) 
    || $_SESSION['basic_is_logged_in'] !== true) {

    // not logged in, move to login page
    header('Location: index.php');
    exit;
}

?>
<?
include 'config.php';
include 'lib/opendb.php';

$query = "SELECT name, gift, got FROM gifts ORDER by name";
$result = mysql_query($query) or die('Error, query failed');

$tsv = array();
$html = array();
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
  foreach ($row as $cKey => $cValue) {
    $row[$cKey] = (preg_match("/[,]+/", $cValue) ? '"' . $cValue . '"' : $cValue);
  }

  $csv[]  = implode(",", $row);
  $html[] = "<tr><td>" .implode("</td><td>", $row) . "</td></tr>";
}

$csv  = implode("\r\n", $csv);
$html = "<table>" . implode("\r\n", $html) . "</table>";

$fileName = 'gifts-to-excel.csv';
header("Content-type: text/csv"); 
header("Content-Disposition: attachment; filename=$fileName");

echo $csv;
//echo $html;

include 'lib/closedb.php';
?>
Return current item: XMasDB