fgetcsv(): Getting data from CSV file

fgetcsv(f_pointer,int $length,string $delimiter, string $encloser,string $escape);
ParameterDESCRIPTION
f_pointerRequired : a successful file pointer fopen()
$lengthOptional : Must be greater than the maximum line length
$delimiterOptional : One char only
$encloserOptional : Field encloser char
$escapeOptional : escape parmeter sets the escape char
Returns a row of data or a record as an array.
CSV (comma separated values ) is a common data storage format, we can get data from a CSV file by using fgetcsv() function in PHP. 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.

Example of fgetcsv()

<?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>";
}
?>
We used open the csv file and keep the pointer
$f_pointer=fopen("student.csv","r"); // file pointer
We will use our student table csv data. ( Download a copy of student.csv file at end of this tutorial )
We used one loop to get 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.

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 )
You can read how the CSV data is prepared from the student table here.


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
String Functions fputcsv(): to write data to a csv file
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer