I am downloading new csv’s each night using a cron job with PHP. Each csv is normally about the same, possibly one night within a month a field is new. I need to get the new field and append it to the database. I don’t know how to get the type of the new field. I saw someone else’s question with gettype() but i’m not sure if that would work or not since the data is inside a csv so wouldn’t they all be strings when some need to be floats, or ints? How would I go across checking the type?
The second question, is there a way to check if there is not a name inside of a table? For instance, if they add a new field called foo52, and I have foo1 through foo51 in my database, is there a quick way to search for fields that aren’t there, or would I have to use a select statement for each one and append it when it’s false?
I use MySQL for my database.
Thanks for your help.
The first question on the part about getting the type is the simply try the conversion of the data itself and then seeing if the data is equivalent with a
==comparison.So,
After reading in the data you can then try the conversion to various types such as strings, ints, ect…then from that you are able to determine the type of the data.
For the second question you can get the column names by doing:
Then you can do a simple
in_arrayto test if the new column name is already in the database.EDIT:
Using
array_diffcan simplify the finding of the missing/new column names from the CSV.