81 Billion

Websites in Singapore that are highly effective and visually appealing

Export mysql data to Excel

Posted on | November 29, 2012 | Comments Off on Export mysql data to Excel

<?PHP
// Original PHP code by Chirp Internet: www.chirp.com.au

include(“source/includes/database.php”);

function cleanData(&$str)
{
// escape tab characters
$str = preg_replace(“/\t/”, “\\t”, $str);

// escape new lines
$str = preg_replace(“/\r?\n/”, “\\n”, $str);

// convert ‘t’ and ‘f’ to boolean values
if($str == ‘t’) $str = ‘TRUE’;
if($str == ‘f’) $str = ‘FALSE’;

// force certain number/date formats to be imported as strings
if(preg_match(“/^0/”, $str) || preg_match(“/^\+?\d{8,}$/”, $str) || preg_match(“/^\d{4}.\d{1,2}.\d{1,2}/”, $str)) { $str = “‘$str”; }

// escape fields that include double quotes
if(strstr($str, ‘”‘)) $str = ‘”‘ . str_replace(‘”‘, ‘””‘, $str) . ‘”‘;
}

// filename for download
$filename = “website_data_” . date(‘Ymd’) . “.xls”;

header(“Content-Disposition: attachment; filename=\”$filename\””);
header(“Content-Type: application/vnd.ms-excel”);

$flag = false;
$result = mysql_query(“SELECT * FROM wp_users ‘”) or die(‘Query failed!’);
while(false !== ($row = mysql_fetch_assoc($result))) {
if(!$flag) {
// display field/column names as first row
echo implode(“\t”, array_keys($row)) . “\r\n”;
$flag = true;
}
array_walk($row, ‘cleanData’);
echo implode(“\t”, array_values($row)) . “\r\n”;
} exit;
?>

 

 

Comments

    About

    This is an area on your website where you can add text. This will serve as an informative location on your website, where you can talk about your site.

    Subscribe to our feed

    Search

    Admin