fgetcsv(): Getting data from CSV file

fgetcsv(f_pointer,int $length,string $delimiter, string $encloser,string $escape);
ParameterDESCRIPTION
f_pointerRequired : a successful file pointer
$lengthOptional : Must be greater than the maximum line length
$delimiterOptional : One char only
$encloserOptional : Field encloser char
$escapeOptional : escape parmeter sets the escape char
We can get data from a CSV file (comma-separated values ) by using fgetcsv() function in PHP. This string function returns us line of data or a record as an array. This function reads the line from the file pointer we provide ( by fopen ) and parse for CSV data. So data can be collected as elements of the array.

Output is indexed array containing the fields read.

Example of fgetcsv()

We will use our student table csv data. ( Download a copy of student.csv file at end of this tutorial )
You can read how the CSV data is prepared from the student table here.
Now the student data is available to us in a csv file

We will first open the csv file and keep the pointer
$f_pointer=fopen("student.csv","r"); // file pointer
Then we will use the pointer to loop through all the rows of the file till the end of the file is reached. Inside the loop we will use fgetcsv() to collect the array and then print the output. Here is the code.
<?php
$f_pointer=fopen("student.csv","r"); // file pointer

while(! feof($f_pointer)){
$ar=fgetcsv($f_pointer);
echo print_r($ar); // print the array 
echo "<br>";
}
?>
The output is here.
Array ( [0] => 1 [1] => John Deo [2] => Four [3] => 75 [4] => female )
Array ( [0] => 2 [1] => Max Ruin [2] => Three [3] => 85 [4] => male )
Array ( [0] => 3 [1] => Arnold [2] => Three [3] => 55 [4] => male )
Array ( [0] => 4 [1] => Krish Star [2] => Four [3] => 60 [4] => female )
Array ( [0] => 5 [1] => John Mike [2] => Four [3] => 60 [4] => female )
Array ( [0] => 6 [1] => Alex John [2] => Four [3] => 55 [4] => male )

Generating a SQL file to insert records into database

Now we can separate data from the above code and using the data we will prepare series of SQL insert commands to store or add all these records to student table. Each loop will have one insert command. Here is the code.
<?php
$f_pointer=fopen("student.csv","r"); // file pointer

while(! feof($f_pointer)){
$ar=fgetcsv($f_pointer);
$sql="INSERT INTO student(id,name,class,mark,sex)values('$ar[0]','$ar[1]','$ar[2]','$ar[3]','$ar[4]')";
echo $sql;
echo "<br>";
}
?>
Part of the output is here
INSERT INTO student(id,name,class,mark,sex)values('1','John Deo','Four','75','female')
INSERT INTO student(id,name,class,mark,sex)values('2','Max Ruin','Three','85','male')
INSERT INTO student(id,name,class,mark,sex)values('3','Arnold','Three','55','male')
INSERT INTO student(id,name,class,mark,sex)values('4','Krish Star','Four','60','female')

Removing First line as column headers

In a csv file we may like not to use first line as data as they will have column headers.
<?php
$f_pointer=fopen("student.csv","r"); // file pointer
$first_line="T";

while(! feof($f_pointer)){
$ar=fgetcsv($f_pointer);
if($first_line<>'T'){
$sql="INSERT INTO student(id,name,class,mark,sex)values('$ar[0]','$ar[1]','$ar[2]','$ar[3]','$ar[4]')";
echo $sql;
echo "<br>";
}
$first_line='F';
}
?>
If you want the sql file to be downloaded then follow the instruction at the end of CSV file creation.

fputcsv(): to write data to a csv file


download the student CSV file student.csv
Visitors Rating
Your Rating





PHP String Functions

Post Comment This is for short comments only. Use the forum for more discussions.




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer