I really do not know how to read line by line csv file and store it in the database.
I want to check datatypes, and the mandatory fields.

Please somebody help me in this

5 Years
Discussion Span
Last Post by diafol

Does that mean you didn't understand what I posted?

DW search = Daniweb search

The search box at the top right of the screen. Type 'csv database' and press 'search'.

You'll probably find what you need there. This is a pretty common question.


Oh. Thank you .
But i am able to get only how to load but not to check datatypes and mandatory fields


OK, by that perhaps isn't such a csv issue.

  1. Load a csv into records or lines (array).
  2. Strip each line into fields, usually using the explode method will give an array.
  3. Test each field from your own array of vaildators.
  4. If they pass the tests, pass on the arrays to an $add_to_db array.
  5. On completion of the validation loop, build a SQL string from the $add_to_db array for inserting into DB. Run query.

You can try to load a csv directly like this:

load data local infile 'mydata.csv' into table MyTable fields terminated by ','
enclosed by '"'
lines terminated by '\n'
(field1, field2, field3)

Although you are then at the mercy of the file contents - no php validation.

You may find the built-in csv functions useful, e.g. http://www.php.net/manual/en/function.fgetcsv.php

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.