load a csv string into mysql

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
corbo950
Forum Newbie
Posts: 13
Joined: Wed May 07, 2008 1:59 am

load a csv string into mysql

Post by corbo950 »

i have some csv data that is in a string the first line has all the colum names and i need 2 know how to put that data into an assocative array based on the column names please help and no the csv is not in a file
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: load a csv string into mysql

Post by Jade »

Sounds to me like you'd need to write something that breaks up the string and dumps it into either a SQL query or into a file that you can then import into mysql. If that's true then you'd need to

1) Extract all the column names (how do you know where the columns start and the data ends? that's your delimiter)

2) Using your delimiter put in an insert statement using the column names and the remaining data

3) Dump it into a file or an SQL statement

4) Import the file or execute the SQL
corbo950
Forum Newbie
Posts: 13
Joined: Wed May 07, 2008 1:59 am

Re: load a csv string into mysql

Post by corbo950 »

ya i got something to extract the data now i just need a function that will replace the keys in an array with an array of new keys:

This is what i have come up with so far :

Code: Select all

<?php
 
    $lines_array = array(
    array("Name", "first"),
    array("John Doe", "John"),
    array("Billy Bob", "Bill")
    );
    $new_lines = array();
    while(list($pos, $line_array) = each($lines_array))
    {
        if($pos == 0)
        {
            $new_keys = $line_array;
        }
        else
        {
            $new_info = $line_array;
            array_push($new_lines, $new_info);
        }
    }
    $count = count($new_keys);
    $new_array = array();
    $new_arrays = array();
    $num = 0;
    while(list($pos, $new_line) = each($new_lines))
    {
        while(list($pos, $attribute) = each($new_line))
        {
            $new_key = $new_keys[$pos];
            $new_array[$new_key] = $attribute;
            array_push($new_arrays, $new_array);
        }
    }
    print_r($new_array);
?>
 
This is what i get back:
Array ( [Name] => John Doe ) Array ( [Name] => John Doe [first] => John ) Array ( [Name] => Billy Bob [first] => John ) Array ( [Name] => Billy Bob [first] => Bill ) Array ( [Name] => Billy Bob [first] => Bill )
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: load a csv string into mysql

Post by Jade »

What are you trying to get? Something like:

Name->first, John Doe->John, Billy Bob->Billy?
corbo950
Forum Newbie
Posts: 13
Joined: Wed May 07, 2008 1:59 am

Re: load a csv string into mysql

Post by corbo950 »

no replace the keys array([0]=>array([name]=>john doe, [first]=>john)[2]=>array([name]=>Billy bob, [first]=>bill)

but i got it figured out thanks though
Post Reply