Usually I’d add a for each loop for a mysql database entry in PHP, but when all the information is in one array (multidimensional i think?)
I’m using http://www.phpclasses.org/quick_csv_import to parse a CSV file which is working fine, but I can’t for the life of me work out how to put the results as they happen in a loop to write to the database.
<?php
$con = mysql_connect("localhost","*****","****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("*****", $con);
require_once('parsecsv.lib.php');
$csv = new parseCSV();
$csv->auto('prod2.csv');
$csv->titles as $value (this has the titles saved in it in an array)
$csv->data as $key => $row (this has the actual values in it in an array)
?>
Edit: The array looks like:
[0] => Array
(
[ProductCategory] => Cleats and Spikes
[ProductSubCategory] => Masters Cleats
[Brand] => Masters
[ItemNumber] => SAM02BY
[ShortCode] => SAM0
[ItemDescription] => Ultra Grip F/T Pk (16) Blk/Yel
[ProductName] => Ultra Grip (6mm, F/T, Q/F)
[Options] => Fast Twist
[ImageURL] => hSAM02BY.jpg
[YoutubeURL] => http://www.youtube.com/watch?v=WuSB6rY9myg
[HostedURL] => 27527890
[StockStatus] => In Stock
[QtyAvailable] => 2149
[RRP] => 8.99
[Barcode] => 5027084199630
[DeliveryDue] => 09/03/2012
)
Any tips will be greatly appreciated
I would load the data directly into MySQL using LOAD DATA INFILE –
This assumes that you have appropriate permissions on the server.
prod2.csvwould need to be replaced with the full path to the file on the server.To process the CSV in PHP you could use something like this –
It is obviously incomplete as you have not included the details of the tables involved. I would advise using
LOAD DATA INFILEif you can, but this will work once you have filled in the blanks. Note that I have written this example using PDO instead of the old mysql_* functions. The repeated execution of the query will have slightly improved performance due to the prepared statement.