Hi,

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

Member Avatar for diafol

How about a DW search on 'csv database'? There are tens of entries. If still stuck, post back.

Am sorry. I did not get DW search

Member Avatar for 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

Member Avatar for diafol

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.