CodeCharge Studio
search Register Login  

Web Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 [SHARED] Working mysql to csv export script

Print topic Send  topic

Author Message
erdcck


Posts: 17
Posted: 08/07/2014, 7:54 AM

Dear friend ı want to share mysql to csv export script for newbies like me :) .

I have put this code submit button custom code in On Click

  
$host = 'localhost'; // MYSQL database host adress  
$db = '********'; // MYSQL database name  
$user = '******'; // Mysql Datbase user  
$pass = '*******'; // Mysql Datbase password  
  
  
// Connect to the database  
$link = mysql_connect($host, $user, $pass);  
mysql_select_db($db);  
   
function exportMysqlToCsv($table,$filename = "jobs.csv")  
{  
	$csv_terminated = "\n";  
	$csv_separator = ",";  
	$csv_enclosed = '"';  
	$csv_escaped = "\\";  
	$sql_query = "select * from $table where user_id=" . CCGetSession("user_id");  
   
	// Gets the data from the database  
	mysql_query("SET NAMES UTF8");  
	$result = mysql_query($sql_query);  
	$fields_cnt = mysql_num_fields($result);  
   
   
	$schema_insert = '';  
   
	for ($i = 0; $i < $fields_cnt; $i++)  
	{  
		$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,  
			stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;  
		$schema_insert .= $l;  
		$schema_insert .= $csv_separator;  
	} // end for  
   
	$out = trim(substr($schema_insert, 0, -1));  
	$out .= $csv_terminated;  
   
	// Format the data  
	while ($row = mysql_fetch_array($result))  
	{  
		$schema_insert = '';  
		for ($j = 0; $j < $fields_cnt; $j++)  
		{  
			if ($row[$j] == '0' || $row[$j] != '')  
			{  
   
				if ($csv_enclosed == '')  
				{  
					$schema_insert .= $row[$j];  
				} else  
				{  
					$schema_insert .= $csv_enclosed .  
					str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;  
				}  
			} else  
			{  
				$schema_insert .= '';  
			}  
   
			if ($j < $fields_cnt - 1)  
			{  
				$schema_insert .= $csv_separator;  
			}  
		} // end for  
   
		$out .= $schema_insert;  
		$out .= $csv_terminated;  
	} // end while  
   
header('Content-Type: text/csv; charset=utf-8');  
header('Content-Description: File Transfer');  
header("Content-Length: " . strlen($out));  
header("Content-Disposition: attachment; filename=$filename");  
header('Expires: 0');  
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');  
header('Pragma: public');  
echo $out;  
exit();  
}  
$table="customer"; // this is the tablename that you want to export to csv from mysql.  
   
exportMysqlToCsv($table,$filename);  
View profile  Send private message
adorni

Posts: 120
Posted: 09/11/2014, 9:45 AM

Do you have any to Excel export? :)
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.