Welcome to PHP4IT
Main Menu
· Main Page
· Account Settings
· Forum
· Recommend Us
· Contact


Search


Solutions Categories
· All topics
· Database Import and Conversion with PHP (Feb 07, 2006)
· PHP Security (Mar 28, 2008)
· Printing (Jan 05, 2006)
· Windows PHP Solutions (Apr 04, 2008)


PHP4IT RSS Feed
Add the PHP4IT RSS Feed to your favorite RSS news reader!

  

Import/Convert DBF files into MySQL - Part 1

Posted by: David on Jan 17, 2006 - 08:13 PM
php-db-conversion 
If you've been presented with the task of having to import a Dbase/DBF - here's a two step way to approach the problem. First the Mysql table structure needs to be created, and then the DBF is then read and inserted into the table.

I'll present step 1 today, which involves the conversion the output from the "disp stru" command in FoxPro into a MySQL create table statement.

Consider a DBF file called "addresses.dbf"- and the "DISP STRU" of the file looks like this:

1 HNAME1 Character 30
2 ADDRESS Character 30
3 EXTRA Character 30
4 CITY Character 28
5 STATE Character 2
6 ZIP Character 10

The convert function would convert that to this:

HNAME1 char (30)
ADDRESS char (30)
EXTRA char (30)
CITY char (28)
STATE char (2)
ZIP char (10)

Here's the script:

<?php
$ddf 
'    1  HNAME1      Character     30
    2  ADDRESS     Character     30
    3  EXTRA       Character     30
    4  CITY        Character     28
    5  STATE       Character      2
    6  ZIP         Character     10
'
;

print 
process_it($ddf);

//takes in string
function process_it($str)
{
    
$new_line '';
    
$lines explode("\n"$str);
    foreach(
$lines as $line){
        
$line substr($line7);
        
$line str_replace('Character''char'$line);
        
$line str_replace('Numeric''int'$line);
        
$elements explode(' '$line);
        foreach (
$elements as $key => $val){
            if (!
strlen(trim($val))){ continue; }
            if (
is_numeric($val)){
                
$newline .= "($val),";
            }else{
                
$newline .= "$val ";
            }
        }
        
$newline .= "\n";
    }
    return 
$newline;
}
?>




Import/Convert DBF files into MySQL - Part 1 | Log-in or register a new user account | 0 Reviews/Comments
Reviews and Comments are opinion statements made by the author.
They do not necessarily represent the opinions of the site editor.