0

I need to extract data from a CSV file and insert it into a MySQL database.

(see sample file below)

So far I have an array with the entire file (reading file with PHP):

Array ([0] => 01 [1] => 12345678X [2] => Title [3] => 120.00 ...etc.

How can I modify the array to create groups of four for each item? Each item is its own array?

For example:

array( array (01,ISBN,Name,Price),
       array (01,ISBN,Name,Price)
      );

Additionally, how can I access those values individually and insert into MySQL?

Ex: $price = 1.25; <--in array

This is what I have so far:

    $fp = @fopen($_FILES['filename']['tmp_name'], "r");

    if ($fp) {

         $arr = array();
         while(!feof($fp)) {
          $this_line = fgets($fp);  

          $line = explode("^",$this_line);

          if($line[0] != "") {          
              array_push($arr,$line[0]);
              }
          $i++;
         }

The above code returns one big array with each column an an element. I would like arrays of four elements per array within one large array so that I can access each "row" individually.

Sample file:

ID 01
Title This is the title
Price 120.00
ISBN xxxxxx
^
ID 02
Title This is the title
Price 20.00
ISBN xxxxxx
^
12
  • 1
    How are the fields separated (per line) within your file? Commented Oct 31, 2011 at 0:11
  • Unfortunately by spaces...so it cannot be easily parsed since the title has spaces in it. Commented Oct 31, 2011 at 0:16
  • @Mr, Jones: One space or multiple spaces? Commented Oct 31, 2011 at 0:19
  • For the specific file I am working with, no, unfortunately spaces :( Commented Oct 31, 2011 at 0:19
  • One space, however the title of the book has spaces in between the title. Commented Oct 31, 2011 at 0:20

1 Answer 1

3

Use fgetcsv, avoid CSV parsing issues.

It will return the file line-by-line, in arrays, which is what you are asking for.

Then you can use MySQLi's prepared statement with bound parameters to insert the lines.

[EDIT] something like this:

$file = fopen('data.csv', 'r');
$stmt = $connection->prepare("INSERT blah blah blah VALUES(?, ?, ?)");
while (($row = fgetcsv($file, 1000, "\t")) !== FALSE) {
  $stmt->bind_param('types string', $row[0], $row[1], etcetera);
  $stmt->execute();
}
Sign up to request clarification or add additional context in comments.

6 Comments

... or PDO, much easier prepare / binding API
This also means you can process one line at a time, instead of trying to slurp the whole file into memory.
@Phil: Can you leave some quick pointers about the PDO stuff and csv?
I just updated my question with the code that I have so far. It gives me each line broken up by column, however it is one big array. How can I create arrays of each line (4 items per array)
@hakre PDO::prepare(), PDOStatement::bindParam(). IMO, a much clearer API for preparing statements and binding parameters than the mysqli offering
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.