

Export data into CSV file in PHP
source link: https://www.laravelcode.com/post/export-data-into-csv-file-in-php
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Export data into CSV file in PHP
You might have already used any package to download data in csv file. If you are using lots of packages in your project, it might sometimes causes version issue. So, it is better to create some functionality without package.
In this article, we will go through export data into CSV file. If you want the data that we have used in example, you can always run the following query in your MySQL command.
CREATE TABLE `visitors` (
`id` int NOT NULL,
`browser` varchar(255) NOT NULL,
`percentage` decimal(10,2) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
INSERT INTO `visitors` (`id`, `browser`, `percentage`) VALUES
(1, 'Chrome', '69.28'),
(2, 'Edge', '7.75'),
(3, 'Firefox', '7.48'),
(4, 'Internet Explorer', '5.21'),
(5, 'Safari', '3.73'),
(6, 'Opera', '1.12'),
(7, 'Others', '5.43');
ALTER TABLE `visitors`
ADD PRIMARY KEY (`id`);
ALTER TABLE `visitors`
MODIFY `id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;
COMMIT;
In the first step, we will connect PHP with database. So create file config.php
and put the below code in it.
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "chart";
// create connection
$connection = new mysqli($servername, $username, $password, $dbname);
// check connection
if ($connection->connect_error) {
die($connection->connect_error);
}
Now create a second file data.php which will get data from sql server. Create data.php
file and insert below code in it.
<?php
include 'config.php';
$sql = "SELECT browser, percentage FROM visitors";
$result = $connection->query($sql);
if ($result->num_rows > 0) {
$data[0][0] = 'Browser';
$data[0][1] = 'Percentage';
$x = 1;
// output data of each row
while($row = $result->fetch_assoc()) {
$data[$x][0] = $row["browser"];
$data[$x][1] = (float)$row["percentage"];
$x++;
}
} else {
die("No records");
}
Now the important part of article, is to create csv file and put data into it. Create index.php
file and insert below code into it.
<?php
include 'data.php';
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="browser.csv"');
$fp = fopen('php://output', 'wb');
foreach ($data as $line) {
fputcsv($fp, $line);
}
fclose($fp);
In the above code, we send header of csv file. Then open the csv file with fopen() command. The second parameter in fopen() function describes write(w) mode of binary(b) data into file. Then fputcsv() function put array data into csv file. In the end we close file fclose() function.
I hope you liked this article. Thank you for giving time in reading the article.
Author : Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Recommend
-
31
Angular Datatable to Export data into Excel, CSV, PDF, Print and Copy I have already shared angular datatable...
-
16
Introduction Wagtail is a modern open source CMS written in Python and based on Django. It is easy to integrate with existing Django projects. Apart from traditional CMS features, it provides a nice...
-
15
The HFT Guy A developer in London Amazon website is limited to 50 instances per page. Viewing lots of instances is a pain and it doesn’t support e...
-
8
Export CSV directly from MySQL Posted: 2008-11-27 - Last updated: 2019-06-05 Tagged
-
11
In this article, we are going to download/export the data in a CSV file from the database using PHP. We are going to use the fopen(), fputcsv() to create the CSV file. Check Output Buffer...
-
7
What Is a CSV File, and How Do I Open It?A Comma Separated Values (CSV) file is a plain text file that contains a list of data. These files are often used for exchanging data between different applications. For example, databases and contact...
-
13
Import a CSV file into Pandas DataFrame – thisPointer A DataFrame is a data structure that stores the data in rows and columns. In this article we will discuss how to import a csv file into a Pandas DataFrame in Python.
-
14
How to Export whole table to CSV using laravel 3569 views 10 months ago Laravel In this article, i will s...
-
6
In this article, we will learn how to Read CSV file into a NumPy Array in Python. Table Of Contents What is CSV File? A CSV file is a comma-separated values file. The csv file format a...
-
7
Posted inSQLMicrosoft4 years ago
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK