How to Export CSV Files Using PHP and MySQL

How to Export CSV Files Using PHP and MySQL

Export data to CSV file from MySQL database using PHP; In this tutorial, we will learn how to export data in CSV file from MySQL database using PHP.

Sometimes we need to export data into MySQL database. The best solution to exporting data into a MySQL database using the PHP script in In CSV file format.

This tutorial helps you an easy way to exporting data into the MySQL database table in CSV file using a PHP script.

Export CSV Files Using PHP and MySQL

Just follow the below steps and easily export csv file into MySQL database using PHP script (code):

  • Step 1 – Create a Database Connection File
  • Step 2 – Export MySQL Data to CSV file PHP Code

Step 1 – Create a Database Connection File

In this step, you will create a file name db.php and update the below code into your file.

The below code is used to create a MySQL database connection in PHP. When we insert form data into MySQL database, there we will include this file:

<?php
	$servername='localhost';
	$username='root';
	$password='';
	$dbname = "my_db";
	$conn=mysqli_connect($servername,$username,$password,"$dbname");
	  if(!$conn){
		  die('Could not Connect MySql Server:' .mysql_error());
		}
?>

Step 2 – Export MySQL Data to CSV file PHP Code

In this step, you need to create one file name export.php and update the below code into your file.

The code below is used to get data from the MySQL database and export it to a CSV file or download it. The sample file name is users-sample.csv.

<?php

	// Database Connection
	require("db.php");

	// get users list
	$query = "SELECT * FROM users";
	if (!$result = mysqli_query($con, $query)) {
	    exit(mysqli_error($con));
	}

	$users = array();
	if (mysqli_num_rows($result) > 0) {
	    while ($row = mysqli_fetch_assoc($result)) {
	        $users[] = $row;
	    }
	}

	header('Content-Type: text/csv; charset=utf-8');
	header('Content-Disposition: attachment; filename=users-sample.csv');
	$output = fopen('php://output', 'w');
	fputcsv($output, array('No', 'Name', 'Mobile', 'Email'));

	if (count($users) > 0) {
	    foreach ($users as $row) {
	        fputcsv($output, $row);
	    }
	}
?>

Conclusion

In this tutorial, we have learned how to export data into the MySQL database in CSV using PHP code.

This is a very basic and easy example of exporting data into the MySQL database in CSV file using PHP code.

If you have any questions or thoughts to share, use the comment form below to reach us.

Recommended PHP Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *