Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7717643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:57:13+00:00 2026-06-01T02:57:13+00:00

Usually I’d add a for each loop for a mysql database entry in PHP,

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:57:14+00:00Added an answer on June 1, 2026 at 2:57 am

    I would load the data directly into MySQL using LOAD DATA INFILE –

    LOAD DATA INFILE 'prod2.csv' INTO TABLE tbl_name
        FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
        LINES TERMINATED BY '\r\n'
        IGNORE 1 LINES;
    

    This assumes that you have appropriate permissions on the server. prod2.csv would 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 –

    <?php
    
    $dsn = 'mysql:dbname=testdb;host=127.0.0.1';
    $user = 'dbuser';
    $password = 'dbpass';
    
    try {
        $dbh = new PDO($dsn, $user, $password);
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
    
    $sql = 'INSERT INTO `table` (ProductCategory, ProductSubCategory, Brand, ...)
            VALUES (?,?,?, ...)
            ON DUPLICATE KEY UPDATE ProductCategory = VALUES(ProductCategory), ProductSubCategory = VALUES(ProductSubCategory), Brand = VALUES(Brand), ...';
    
    $sth = $dbh->prepare($sql);
    
    require_once 'parsecsv.lib.php';
    $csv = new parseCSV();
    $csv->auto('prod2.csv');
    
    foreach ($csv->data as $row) {
    
        // bind params and execute query
        $sth->execute(array($row['ProductCategory'], $row['ProductSubCategory'], $row['Brand']));
    
    }
    

    It is obviously incomplete as you have not included the details of the tables involved. I would advise using LOAD DATA INFILE if 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Usually i use SQL Server as database for my project, but one of my
Usually when I build a site, I put all the CSS into one file,
Usually I make my Regex patterns by myself, but I can't figure this one
Usually, when sending a http status header from PHP, one would send the HTTP
Usually, to export CSV reports from a mysql server, I connect & run a
Usually wordpress structure for category is as below. http://ourdomain/category/categoryname/subcategoryname but I want my structure
Usually JAXB works but it is not parsing my schema correctly or something as
Usually I'm good with this type of thing, but this is bugging me. I
Usually ‘expandable’ grids are represented as list of lists (list of rows, each row
usually , we use http get method to do a spellcheck query, but now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.