el codigo no es mio pero funciona bastante bien, lo que hace es leer un fichero sql que este en un directorio y crea la tabla o carga los datos en esta:
archivo 1:
Código PHP:
Ver original<?php
@include_once('phpMyImporter.php');
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "test";
$path = "http://127.0.0.1/TEST/";
$filename = $path."test.sql"; // Filename of dump, default: dump.sql
$compress = false; // Import gz compressed file, default: false
$dump = new phpMyImporter($dbname,$connection,$filename,$compress);
$dump->utf8 = true; // Uses UTF8 connection with MySQL server, default: true
$dump->doImport();
?>
librearia:
Código PHP:
Ver original<?php
class phpMyImporter {
/**
* @Acceso privado
*/
var $database = null;
var $connection = null;
var $compress = null;
var $utf8 = null;
var $importFilename = null;
function phpMyImporter($db=null, $connection=null, $filepath='test.sql', $compress=false) {
$this->connection = $connection;
$this->compress = $compress;
$this->importFilename = $filepath;
$this->utf8 = true;
return $this->setDatabase($db);
}
/**
* Sets the database to work on
* @param string $db The database name
*/
function setDatabase($db){
$this->database = $db;
return false;
return true;
}
/**
* Read from SQL file and make sql query
*/
function importSql($file) {
// Reading SQL from file
echo "Leyendo instrucción SQL desde archivo '".$this->importFilename."':<br><br> ";
if ($this->compress) {
}
else {
}
echo " Finalizada la Lectura!<br><br>\n";
echo "Importando SQL dentro de tu base de datos '".$this->database."':<br><br> ";
$x = 0;
$importSql = "";
$procent = 0;
foreach ($lines as $line) {
// Print progress
$x++;
$numOfLines = count($lines); if ($x%(int)($numOfLines/20) == 0) {
$procent += 5;
if ($procent%25 == 0) echo "$procent%";
else echo ".";
}
// Importing SQL
$importSql .= $line;
if (!$query) return false;
$importSql = "";
}
}
return true;
}
/**
* Import SQL file into selected database
*/
function doImport() {
if ( !$this->setDatabase($this->database) )
return false;
if ( $this->utf8 ) {
$encoding = @mysql_query("SET NAMES 'utf8'", $this->connection); }
if ( $this->importFilename ) {
$import = $this->importSql($this->importFilename);
if (!$import) echo "\n".mysql_error($this->connection)."\n"; else echo "<br><br> Terminado, Archivo Importado!\n";
return $import;
}
else {
return false;
}
}
}
?>