How to Export data to CSV file from Database using XAMPP ? Last Updated : 09 May, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we are going to load the data present in the database (MySQL) into CSV file. Display the data in the database and also export that data into CSV file. We are using XAMPP tool to store locally in a database. XAMPP stands for cross-platform, Apache, MySQL, PHP, and Perl. It is among the simple light-weight local servers for website development. Steps: Create database and create table in your XAMPP server(MySQL ) database.Write PHP Script to access that dataVerify results Scenario with CSV: Create two PHP scripts named index.php and export.php Place these files in xampp/htdocs/folder/ index.php: Display the columns along with data using mysqli_fetch_array. The fetch_array() / mysqli_fetch_array() function fetches a result row as an associative array, a numeric array, or both. Execution Steps: 1. Open XAMPP Server and start Apache and MySQL server 2. Create database with name "article_geeksdata" 3. Create table inside this database named "geeksdata" and insert data Open Notepad and type the following code. Save this file as index.php PHP <?php // Connect syntax $connect = mysqli_connect("localhost", "root", "", "article_geeksdata"); // Display data from geeksdata table $query ="SELECT * FROM geeksdata"; // Storing it in result variable $result = mysqli_query($connect, $query); ?> // HTML code to display our data // present in table <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"> </script> <link rel="stylesheet" href= "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> <script src= "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> </script> </head> <body> <div class="container" style="width:900px;"> <h2 align="center">Geeks for Geeks</h2> <h3 align="center"> Export data into CSV from Data Base </h3> <br /> <!-- After clicking on submit button export.php code is revoked --> <form method="post" action="export.php" align="center"> <input type="submit" name="export" value="CSV Export" class="btn btn-success" /> </form> <br /> <!-- Code for table because our data is displayed in tabular format --> <div class="table-responsive" id="employee_table"> <table class="table table-bordered"> <tr> <th width="5%">UserID</th> <th width="35%">Name</th> <th width="10%">Article</th> <th width="20%">Article Type</th> <th width="5%">Published Date</th> </tr> <?php // Fetching all data one by one using // while loop while($row = mysqli_fetch_array($result)) { ?> <!-- taking attributes and storing in table cells --> <tr> <!-- column names in table --> <td><?php echo $row["user_id"]; ?></td> <td><?php echo $row["name"]; ?></td> <td><?php echo $row["article"]; ?></td> <td><?php echo $row["article_type"]; ?></td> <td><?php echo $row["published_date"]; ?></td> </tr> <?php }?> </table> <!-- Closing table tag --> </div> <!-- Closing div tag --> </div> </body> </html> Output of this web page Now open another page in notepad and type the following code. Save this file as export.php PHP <?php // Checking data by post method if(isset($_POST["export"])) { // Connect to our data base $connect = mysqli_connect("localhost", "root", "", "article_geeksdata"); // Accept csv or text files header('Content-Type: text/csv; charset=utf-8'); // Download csv file as geeksdata.csv header('Content-Disposition: attachment; filename=geeksdata.csv'); // Storing data $output = fopen("php://output", "w"); // Placing data using fputcsv fputcsv($output, array('User_ID','Name', 'Article_Name', 'Article_Type', 'Data_published')); // SQL query to fetch data from our table $query = "SELECT * from geeksdata"; // Result $result = mysqli_query($connect, $query); while($row = mysqli_fetch_assoc($result)) { // Fetching all rows of data one by one fputcsv($output, $row); } // Closing tag fclose($output); } ?> Output: EXECUTION VIDEO Comment More infoAdvertise with us Next Article How to Export data to CSV file from Database using XAMPP ? sravankumar_171fa07058 Follow Improve Article Tags : PHP HTML-Misc PHP-Misc Similar Reads How to fetch data from the database in PHP ? Database operations in PHP are a very crucial thing that is especially needed in CRUD (Create, Read, Update and Delete) operations. In this article, we will discuss the Read part i.e. data fetching from database. There are two ways to connect to a database using PHP. They are as follows. MySQLi ("i" 4 min read How to retrieve data from MySQL database using PHP ? There are steps to understand for retrieving the data from the MySQL database. Approach: Create the database, then create the table for data.Enter the rows in the table. You have to connect to the database. Now we understand each and every step as shown below.  Example 1: In this. we use PHPMyAdmin 2 min read How to Insert JSON data into MySQL database using PHP? To insert JSON data into MySQL database using PHP, use the json_decode function in PHP to convert JSON object into an array that can be inserted into the database. Here, we are going to see how to insert JSON data into MySQL database using PHP through the XAMPP server in a step-by-step way. JSON Str 3 min read How to Export WordPress Database Using phpMyAdmin? Exporting your WordPress database is a must for backing up your website data, migrating your site to a new server or domain, or creating a local copy for development purposes. phpMyAdmin, a popular tool included in most hosting control panels, makes this process simple and accessible. Here, we will 6 min read How to Update Data in MySQL Database Table Using PHP? Updating data in a MySQL database table using PHP is a fundamental aspect of web development, particularly in applications where user interactions involve modifying existing records. This guide delves into the process of updating data in a MySQL database table using PHP, covering database connection 3 min read DataBase Operations through XAMPP Console In this article, we are going to perform database operations in My SQL Xampp Server using Xampp tool. We will perform the following operation as follows. Creating databaseCreate a table in databaseInsert data into itView data.Requirements -Xampp serverIntroduction :Database is a collection of relate 2 min read How to generate an XML file dynamically using PHP? A file can be generated using PHP from the database, and it can be done by the static or dynamic method in PHP. Static methods can be called directly - without creating an instance of a class. Here we are going to discuss how to create an XML file dynamically.The first thing we need to do is fetch t 2 min read How to create a table in PDF file from external text files using PHP ? In this article, we will learn to create a table in a PDF file from an external text file with PHP by using FPDF. It is a free PHP class that contains many functions for creating and modifying PDFs. The FPDF class includes many features like page formats, page headers, footers, automatic page break, 3 min read How to configure XAMPP to send mail from localhost using PHP ? To configure XAMPP server to send mail from localhost we have to make changes to two files sendmail.ini and php.ini. Open the xampp folder. By the name of "sendmail.ini" is present in sendmail file and "php.ini" is present in php file in the xampp folder. Step 1: Go to C:\xampp\sendmail: open sendma 2 min read How to fill all input fields automatically from database by entering input in one textbox using PHP ? XAMPP is a free and open-source cross-platform web server solution developed by Apache Friends, consisting mainly of the Apache HTTP Server, MySQL database, and interpreter for scripts written in the PHP programming language. The XAMPP server helps to start Apache and MySQL and connect them with the 4 min read Like