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($line, 7);
$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;
}
?>