Import from csv to db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

greg7
Forum Commoner
Posts: 32
Joined: Tue Oct 13, 2009 7:38 am

Re: Import from csv to db

Post by greg7 »

Hi weiry, i didnt fix it, i found adolikecsvreader-2009-06-10 from php classes, any help?
greg7
Forum Commoner
Posts: 32
Joined: Tue Oct 13, 2009 7:38 am

Re: Import from csv to db

Post by greg7 »

I solved it!!

Code: Select all

 
//----------------------------------------------------------------------------------------------
//----------------------------INSERT INTO STATEMENT----------------------------
//----------------------------------------------------------------------------------------------
 
// Generate base query
$base_query = "INSERT INTO inouts (`id`";
 
foreach ($cols as $col)
   {         
        $base_query .= ", ";
        $base_query .= "`{$col}`";
    }
$base_query .= ") ";
 
$j=1;
foreach ($csv->data as $key => $row)
{
    $value_query = "VALUES ('{$j}'";
    foreach ($row as $value)  
    {
        $value_query .= ", ";   
        $value_query .= "'$value'";
    }
    $value_query .= ")";
            
    // Combine generated queries to generate final query
    $query = $base_query .$value_query .";";
    echo "$query <br/>";
        
    // Execute query
    $result=mysql_query($query,$link); 
        
    //Check if query executed
    if ($result) {
         echo "table created"; }
    else { 
        echo "Error creating table:" . mysql_error()."<br><b><font color=red>\n</b>"; }
     $j++;   
}
 
//Close connection
mysql_close($link);
 
Thanks a lot weiry for help!
Post Reply