المشاركات

عرض الرسائل ذات التصنيف CSV

How to Create and Download CSV File in PHP

صورة
Hi! Here we will see how to create a csv file and download it using php . CSV is one of the popular data storage methods used on the Web. Being a modern language, PHP has no problems handling various data formats including csv. It offers native functions to read and write csv files. With fputcsv() method, you can write data as a csv file and force it to download. Clicking on a file url (link) will just open it in the browser window without downloading. The exe and zip formats are an exception here. But in case you need to download it directly to client's hard disk, then you have to make use of the readfile() function. Let's see how to do it. PHP - Create CSV File: The following snippet creates a csv file named 'myfile.csv' on your current working directory. <?php // data array $user = array(1, 'Johnson', 'johnson@mydomain.com', 'Miami'); // filename $filename = 'myfile.csv'; // write to csv file $fp = fopen($filename, 'w...

How to Import CSV File into MySQL with LOAD DATA INFILE

صورة
I tried to import a csv file into mysql with php script . It is a simple solution if you have a small set of data to import. But if you have hundreds of thousands of records, don't even think about it. You need something different to read through a large dataset quickly. MySQL's LOAD DATA INFILE command works like a charm and can be executed from the command line. Using LOAD DATA INFILE allows you to load csv or any other delimited data file to the mysql table with a single command. It comes with several options and we will see below what they are and how to use them to import the csv data set into the mysql table. Using LOAD DATA INFILE Command: The LOAD DATA INFILE command in mysql loads data from a file into the table at high speed. The file path specified must be absolute or relative. 1. Importing CSV File into MySQL: Let's say we have a mysql table 'customers' with the following structure. MySQL Table: Customers CREATE TABLE `customers`( `id` INT...

Convert JSON to CSV using PHP (JSON Keys as Column Headers)

صورة
Hi! Here's how to convert json to csv in php by using json keys as column headers for csv file . Even though json format is immensely popular for data exchange over web, there are times you may need to work with csv data and have to convert json to csv. Being a modern language, php doesn't have trouble handling both data formats but unfortunately there's no one step solution to convert json to csv using php . But don't worry! I have created a simple php script for json to csv conversion. In case you don't know, json stores data as 'key:value' pairs. And the function I have created will take valid json keys and use it as column headers for the csv file during conversion. I have already discussed about converting csv to json in php and below we'll see about converting json to csv in php. But keep in mind there are so many things can go wrong during this conversion so you need to do proper error handling in the script. How to Convert JSON to CSV ...

Convert CSV To JSON Using PHP (With Header Row As Keys)

صورة
Hi! Here is the php script to convert csv to json with header row as keys. Both csv and json are used to store data but json is little more verbose and human readable and has become popular data exchange format among modern web services. Converting csv data into json format is little tricky especially if you want your json to be properly formatted as key:value pairs. The csv header row should be used as keys and the subsequent rows as values to form json string. Unfortunately there is no direct way to do this but likely there is a workaround to bring out the desired result. The method used here is by far the most effective solution to convert csv to json with header row as keys. Converting CSV to JSON using PHP Let's take a sample csv file with header and some rows and convert it to json using php script. Data.csv Id,Name,Position,Salary 1,Suki Burks,Developer,$114500 2,Fred Zupers,Technical Author,$145000 3,Gavin Cortez,Team Leader,$235500 PHP Function to Convert CSV...

Import CSV File into MySQL Database using PHP Script

صورة
Hi, this time I have come up with another useful php script, which will show you How to Import CSV File Data into MySQL Database using PHP . CSV Files commonly known as comma separated values is one of the most preferred data storage methods. And as a developer often you might find you in situation to import those huge csv data into database system. Though sounding simple, it's a nightmare in disguise. Here's where php comes to the rescue. With few lines of code, you can insert several hundreds of records in flash. PHP supports several data formats like csv, json etc. and provides dedicated functions to handle such data formats. Hence without any third party solutions, we can to parse and store csv records in database using core php alone. Read Also How to Convert CSV To JSON File using PHP How to Import JSON File into MySQL using PHP How to Export MySQL to JSON File using PHP Create Sample MySQL Database As an example, let's use our previous 'library...