maccaj51
asked on
PHP: Csv to Mysql
Hi Experts,
I have the following code which takes a csv file and displays it as a table.
What I would like to do it is take all rows after row #1 and insert it into a mysql database.
Could someone please help me with this
Many Thanks
I have the following code which takes a csv file and displays it as a table.
What I would like to do it is take all rows after row #1 and insert it into a mysql database.
Could someone please help me with this
Many Thanks
ini_set("auto_detect_line_endings", true);
$csv = test.csv;
$fpo = fopen($csv, 'r');
if (!$fpo) die("CANNOT OPEN $csv");
$top = fgetcsv($fpo);
// READ THE REST OF THE ROWS
$arr = array();
while (!feof($fpo))
{
$arr[] = fgetcsv($fpo);
}
// CREATE AN HTML DOCUMENT
$htm = '<h2>' . $csv . '</h2>' . PHP_EOL;
// CREATE A TABLE
$htm .= '<table width="100%">' . PHP_EOL;
// THE TOP ROW IN BOLD
$htm .= '<tr>' . PHP_EOL;
foreach ($top as $dat)
{
$htm .= '<td>' . "$dat" . '</td>' . PHP_EOL;
}
$htm .= '</tr>' . PHP_EOL;
// THE OTHER ROWS
foreach ($arr as $row)
{
if (empty($row))
{}
else
{
$htm .= '<tr>' . PHP_EOL;
foreach ($row as $dat)
{
$htm .= '<td>' . $dat . '</td>' . PHP_EOL;
}
$htm .= '</tr>' . PHP_EOL;
}
}
// CLOSE THE HTML DOCUMENT
$htm .= '</table>';
echo $htm;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Ray,
Thanks so much for that script...
It seems to be doing everything it should... including echoing the correct import.
However it doesnt insert into the database...
Thanks so much for that script...
It seems to be doing everything it should... including echoing the correct import.
However it doesnt insert into the database...
ASKER
Attached is a csv files with the headings for the fields...
I have added this code to the top of the php script:
ini_set("auto_detect_line_ endings", true);
And it outputs the records followed by correct number of "RECORDS PROCESSED"
But doesnt insert them into the table.
Am I doing something wrong?
Many Thanks
Workbook2.csv
I have added this code to the top of the php script:
ini_set("auto_detect_line_
And it outputs the records followed by correct number of "RECORDS PROCESSED"
But doesnt insert them into the table.
Am I doing something wrong?
Many Thanks
Workbook2.csv
@maccaj51: The code I furnished is a teaching example. I do not have your data base and tables set, so there is no way for me to run the query. So the answer is , "No, you're not doing anything wrong." You just have to go into the script, find the code on line 85 and 86 and add your own call to your query handler at that point.
ASKER
All sorted Ray... Many thanks
Great! Thanks for the points and thanks for using EE, ~Ray
just an eg
mysql> LOAD DATA LOCAL INFILE ‘/var/www/site/site_users.
INTO TABLE site_users FIELDS
TERMINATED BY ‘;’
ENCLOSED BY ‘”‘
LINES TERMINATED BY ‘\n’
IGNORE 1 LINES;