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.

Read Also:  CodeIgniter CRUD Example Tutorial From Scratch

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

<?php

$data_list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');

foreach ($data_list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);
?>

Method 1 – example export csv file in php mysql

<?php

$db_record = '98*****856';

$where = 'WHERE 1 ORDER BY 1';
// filename to be exported
$csv_filename = 'db_export_'.$db_record.'_'.date('Y-m-d').'.csv';

$host_name = "localhost";
$user_name = "98*****856";
$db_pass = "98*****856";
$db_name = "98*****856";
// Database connecten voor alle services
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());


$csv_export = '';

// query to get data from database
$query = mysql_query("SELECT * FROM ".$db_record." ".$where);
$field = mysql_num_fields($query);

// create line with field names
for($i = 0; $i < $field; $i++) {
  $csv_export.= mysql_field_name($query,$i).';';
}

// newline (seems to work both on Linux & Windows servers)
$csv_export.= '
';

// loop through database query and fill export variable
while($row = mysql_fetch_array($query)) {
  // create line with field values
  for($i = 0; $i < $field; $i++) {
    $csv_export.= '"'.$row[mysql_field_name($query,$i)].'";';
  }  
  $csv_export.= '
';  
}

// Export the data and prompt a csv file for download
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=".$csv_filename."");
echo($csv_export);

?>

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

<?php 
 
$url = 'all-pakainfo-website-url-list.csv'; 

$file_name = basename($url); 

$info = pathinfo($file_name); 

if ($info["extension"] == "csv") { 
	if(file_put_contents( $file_name, 
			file_get_contents($url))) { 
		echo "File downloaded successfully"; 
	} 
	else { 
		echo "File downloading failed."; 
	} 
} 
else echo "Sorry, that's not a CSV file"; 

?> 

Example 2: use of readfile() function

<?php 

$url = "all-pakainfo-website-url-list.csv"; 

echo "Processing- Your file is being checked. <br>"; 

$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.

Read Also:  Login Signup with Laravel

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel 6 drop column if exists MySQL using Migration
  2. Read Also:  crud operations in php
  3. Read Also:  How to parseInt() Function convert a string to an integer in javascript?
  4. Read Also:  jQuery Delete Confirm in Laravel 5.7 Example
  5. Read Also:  How to Insert Data Into MySQL Database Table Using PHP?
  6. Read Also:  vue Image Upload Component
  7. Read Also:  PHP Magento extension soap must be loaded error
  8. Read Also:  Export data mysql table to csv file using PHP
  9. Read Also:  Display Uploaded Image Using Javascript Example
  10. Read Also:  How to parseInt() Function convert a string to an integer in javascript?