download csv file in php example

Today, We want to share with you download csv file in php example.In this post we will show you export mysql query to csv using php, hear for php export table to csv we will give you demo and example for implement.In this post, we will learn about Export data mysql table to csv file using PHP with an example.

example export csv file in php mysql

In this tutorial we learn to all about download csv file in php example Examples like as a download csv file in php, example export csv file in php mysql, export data from mysql table to csv file using php, export mysql query to csv using php, php export table to csv, php export to csv, php export to csv download or many more.

How to export data in csv file using php and mysql?


Method 1 – example export csv file in php mysql



Method 2 : export data from mysql table to csv file using php

$host_name = "localhost";
$user_name = "98*****856";
$db_pass = "98*****856";
$db_name = "98*****856"; 

$connection = mysql_connect($host_name , $user_name , $db_pass) or die('Could not connect: ' . mysql_error()); mysql_select_db($db_name ) or die ('Could not select database ' . mysql_error());

$sql = mysql_query("SELECT * FROM ".$db_record."  WHERE 1 ORDER BY 1");

$res = mysql_query($sql,$conecction);
$file= fopen('file.csv', 'w');
while($row = mysql_fetch_assoc($res)){
fputcsv($file, $row);
}
fclose($file);

Example 1: Using file_get_contents() function

 

Example 2: use of readfile() function

"; 

$file_name = basename($url); 

$info = pathinfo($file_name); 

if ($info["extension"] == "csv") { 
	header("Content-Description: File Transfer"); 
	header("Content-Type: application/octet-stream"); 
	header( 
	"Content-Disposition: attachment; filename=\""
	. $file_name . "\""); 
	echo "Good Luck, File downloaded successfully"; 
	readfile ($url); 
} 

else echo "Sorry, that's not a CSV file"; 

exit(); 

?> 

I hope you get an idea about download csv file in php example.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment