Created
December 4, 2012 04:01
-
-
Save ricardodantas/4200454 to your computer and use it in GitHub Desktop.
dbf2mysql_2.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Path to dbase file | |
$db_path = "dbftable.DBF"; | |
// Open dbase file | |
$dbh = dbase_open($db_path, 0) | |
or die("Error! Could not open dbase database file '$db_path'."); | |
// Get column information | |
$column_info = dbase_get_header_info($dbh); | |
$nfldcount = count($column_info); | |
//create the sql table structure f | |
$sql_database = "mysql_database"; | |
$sql_table_name = "mysql_table "; | |
$sql = "Create Table ".$sql_table_name ."("; | |
$flds =""; | |
$comma = ","; | |
for($i=0;$i<$nfldcount;$i++){ | |
if($i == $nfldcount-1) | |
$comma = ""; | |
$datatype = get_sql_data_type($column_info[$i]["type"] ,$column_info[$i]["length"], $column_info[$i]["precision"] ); | |
$flds .="`". $column_info[$i]["name"] ."` ". $datatype .$comma; | |
} | |
$stru = $sql. $flds .")Engine = InnoDb "; | |
// connect to mysql | |
$link = mysql_connect("localhost", 'root', ''); | |
if (!$link) { | |
die('Could not connect: ' . mysql_error()); | |
} | |
// select the database -- database should be already exist. | |
$db_selected = mysql_select_db($sql_database, $link); | |
if (!$db_selected) { | |
die ('Can\'t use : '.$sql_database . mysql_error()); | |
} | |
$result = mysql_query($stru,$link); | |
if($result){ | |
echo("Table Created successfully"); | |
} | |
// count total records of dbf file | |
$dbf_total_records = dbase_numrecords($dbh); | |
// dbf record number starts with 1 and not with 0 so take first values as 1 | |
$insert_sql = "Insert into ".$sql_table_name. "Values(" ; | |
for($k=1; $k<=$dbf_total_records;$k++){ | |
$row = dbase_get_record($dbh, $k); | |
$comma = ","; | |
$values=""; | |
for($x = 0;$x 0){ | |
$len = $dbf_length + $dbf_decimal + 1 ; // total length + decimal places + 1 decimal point | |
$sdatatype = 'float('. $len .')'; | |
} | |
else | |
$sdatatype = 'int('.$dbf_length.')'; | |
break; | |
case 'date': | |
$sdatatype = 'date '; | |
break; | |
case 'character': | |
$sdatatype = 'varchar('.$dbf_length.')'; | |
break; | |
case 'boolean': | |
$sdatatype = 'tinyint (1)'; //'binary(1)'; // purposly done binary is difficut to retrive and use | |
break; | |
case 'memo': | |
$sdatatype = 'longtext '; | |
break; | |
} | |
return $sdatatype; | |
} | |
function insert_record($sql_table_name, $values,$link){ | |
set_time_limit(30); // Increase the script execution time on every insert | |
$ins_sql = "insert into " .$sql_table_name. " values (". $values . ")"; | |
$result = mysql_query($ins_sql,$link); | |
if(!$result) { | |
echo($ins_sql); | |
echo("record not inserted"); | |
return false; | |
} | |
return true; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment