Our blog
Build Table from File PHP Function
This little PHP function will allow you to import a csv or tab etc delimited text file into a database table. Handy if you need it
-
function build_table_from_file($tablename, $filepath, $delim="\t") {
-
db_query("DROP TABLE IF EXISTS $tablename");
-
$headers=false;
-
if($delim!='csv'){
-
}
-
if(!$headers) {
-
foreach($r as $h){
-
}
-
$sql = "CREATE TABLE $tablename
-
(
-
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,";
-
foreach($headers as $h) {
-
$sqls[]=" `" . db_in($h) . "` TEXT NOT NULL ";
-
}
-
) ENGINE = MYISAM ";
-
db_query($sql);
-
continue;
-
}
-
$sql = "insert into $tablename set ";
-
foreach($headers as $k=>$h) {
-
$sqls[] = "`$h` = '" . db_in($r[$k]) . "'";
-
}
-
db_query($sql);
-
pbar();
-
}
-
}
RSS Feed