Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8944843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:06:41+00:00 2026-06-15T12:06:41+00:00

I am trying to read a CSV file using PHP. I want to use

  • 0

I am trying to read a CSV file using PHP. I want to use a while statement for each line, and would like to assign a variable for each column. The reason I am assigning variables for each column is so that I can use those variables within a sql query.

$csv = fopen('sample2.csv','r') or die("can't open file");
while($csv_line = fgetcsv($csv,1024)) {
    //Not sure what to do here
}
fclose($csv) or die("can't close file");

I understand how to open the file and read the overall line, but not how to control the individual columns.

Updated Code

<?php
    $csv = fopen('sample.csv','r') or die("can't open file");
    while($csv_line = fgetcsv($csv,1024)) {
        list($column1, $column2, $column3) = $csv_line;
        echo 'Column 1: '.$column1.'<br />';
        echo 'Column 2: '.$column2.'<br />';
        echo 'Column 1: '.$column1.'<br />';
        echo 'Column 3: '.$column3.'<br />';
    }
    fclose($csv) or die("can't close file");
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:06:43+00:00Added an answer on June 15, 2026 at 12:06 pm

    fgetcsv returns an array with each field in it. You can use the list function to get all columns in that array:

    while($csv_line = fgetcsv($csv,1024)) {
        list($column1, $column2, $column3) = $csv_line;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read a 200 MB csv file using SQLalchemy. Each line
I am trying to use the CSVhelper plugin to read an uploaded CSV file.
I am trying to read a very simple but somehow large(800Mb) csv file using
To simplify, I'm trying to read the content of a CSV-file using the ifstream
I am trying to read only the first line of a zipped csv file.
I can't really read PHP but I'm trying to upload a csv file in
I am trying to read a file using csv.DictReader I have a field that
I am trying to read a CSV file in R (under linux) using read.csv().
Using Android (Eclipse) I am trying to read a .csv file and dynamically populate
I am trying to create a import tool that would read the csv file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.