What is export and import in php?

PHP is widely used for building a wide range of products ranging from web apps to enterprise level applications. The key to efficient PHP code is to follow proper workflows and automate processes. The result is high quality and bug-free code.

In almost all PHP applications, data is stored, accessed and exchanged between various components of the app. To make sure that this exchange and access to data goes smoothly and without any issues, the development team must make sure that the databases and data dumps are in proper format.

Import and export of data to and from databases is a common enough procedure in PHP development. Another important activity is the backup and transfer of databases.

In this article, I will explain how to save tables from CSV files to MySQL and vice versa. You need to signup at Cloudways to launch a server and PHPstack application. Before signing up, looking at all the pricing options from the world-class hosting providers like AWS, DigitalOcean, Linode, Vultr and GCP is a good idea so you can find the one that perfectly fits your needs.

PHP Hosting: Best PHP 7 & PHP 5.6 Web Hosting

Create a Database in MySQL

The first step in this tutorial is the creation of a MySQL database. Since Cloudways provides the custom mysql manager in the platform which contains a database for app. you can create tables by running SQL queries. Create a table `employeeinfo` in database using the following SQL query.

CREATE TABLE employeeinfo[
emp_id VARCHAR[50] UNSIGNED PRIMARY KEY,
firstname VARCHAR[30] NOT NULL,
lastname VARCHAR[30] NOT NULL,
email VARCHAR[50],
reg_date VARCHAR[50]
]

This will create a new table `employeeinfo` in the database. I will use this table to insert data from the CSV file.

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

Create MySql Connection in PHP

For importing and exporting database in MySql will make a separate file `config.php`. Add the following code and replace the database credentials with yours. You can find your db credentials in Application Access details:


Related: How To Connect MySQL Database With PHP Websites

Import CSV to MySQL in PHP

After the database has been created, I next need an HTML file  that could upload CSV file. For this HTML file, I will use HTML File uploader in a simple bootstrap form.

Create a file and name it `index.php` . This is a simple form for uploading CSV file. This file will also show the results in a simple table on the same page. When the user submits the form,  all records will be saved in the database.

First, I will add Bootstrap CDN to index.php.



Next, in the `body` tag,  add the following HTML code for the Bootstrap form.




    
    
    




    
Form Name
Select File
Import data
Import

You might notice that I have set an action to `functions.php` file. In the next step, I will create this file and add code to it. I have also include a method `get_all_records[]` near the end of the file. This method fetches all the records from the database and display the records in the table on the index page.

Next up, I will create `functions.php` file and add the following code in it.

Chủ Đề