Export CSV File Data to MySQL Using PHP

In PHP, the fopen() and fgetcsv() functions help developers to add data in an excel file from a MySQL database.

In this tutorial, we will show you how to export data in CSV file from MySQL database using PHP.

How to Export CSV File to MySQL Database Using PHP?

Just follow the below steps and easily export csv file into MySQL database using PHP script (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.

<?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());
		}
?>

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:

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.

<?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);
	    }
	}
?>

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.

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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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