Look at fopen(), fread(), str_explode() and mysqli.
You can use fopen/fread to open your file and read it line by line, str_explode to grab your csv elements in each line as an array, and mysqli to store it.
How can read a CSV file, Convert the columns in CSV to rows and save it into a database table using PHP. The number of columns is dynamic.
Why would you even want to do this? DB columns are not dynamic, and there is no reason to turn rows into columns and columns into rows. This would be a good php brain teaser though.
Why would you even want to do this? DB columns are not dynamic, and there is no reason to turn rows into columns and columns into rows. This would be a good php brain teaser though.
---
I am clear till the point a import the data and add it to an array using php explode statement. I need help on the next part how do i convert the columns into rows, I have to leave out the first column and convert all the columns from the second onwards to rows. The reason is that the first column in the CSV is the header for the target table in mysql.
The CSV file contains the data for different models of the same type of machine and same type of parts, the parts of every model are put in different columns in CSV file i have to convert them into rows and insert to mysql table.
The example data in the csv file is as below:
Type B1 C1 C2
Motor 23A 23A 23U
Fins FRC FRC FRZ
Gear 3B12 3B12 3B33
Handle XL XL XL
Pully Normal Normal Normal
In the above data, the first column will be the table haeder in mysql table, so we ignore it while insering to the mysql table we take only from the second column. Hope i am clear.
Type B1 C1 C2
Motor 23A 23A 23U
Fins FRC FRC FRZ
Gear 3B12 3B12 3B33
Handle XL XL XL
Pully Normal Normal Normal
I suggest rewrite the data to a new CSV file formatted correctly, then insert into a database. The code below appears to be OK, but will need additional code to remove the last comma or whatever CSV character you use.