Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading .csv file in php

Tags:

php

csv

I want to read .csv file in PHP and put its contents into the database. I wrote the following code:

$row = 1;
$file = fopen("qryWebsite.csv", "r");
while (($data = fgetcsv($file, 8000, ",")) !== FALSE) {
    $num = count($data);
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "\n";}}
fclose($file);

I do not get any error but it does not show me result.

like image 362
Neha Raje Avatar asked Mar 28 '11 07:03

Neha Raje


People also ask

How do I read a CSV file in column wise in php?

You can open the file using fopen() as usual, get each line by using fgets() and then simply explode it on each comma like this: <? php $handle = @fopen("/tmp/inputfile. txt", "r"); if ($handle) { while (($buffer = fgets($handle)) !==

How do I display the content of a CSV file in HTML?

To display the data from CSV file to web browser, we will use fgetcsv() function. Comma Separated Value (CSV) is a text file containing data contents. It is a comma-separated value file with . csv extension, which allows data to be saved in a tabular format.

How do I view data in a CSV file?

Approach of the program: Import required libraries, matplotlib library for visualization and importing csv library for reading CSV data. Open the file using open( ) function with 'r' mode (read-only) from CSV library and read the file using csv. reader( ) function. Read each line in the file using for loop.


1 Answers

I am using parseCSV class to read data from csv files. It can give more flexibility in reading csv file.

like image 50
Shameer Avatar answered Sep 21 '22 05:09

Shameer