Ver Mensaje Individual
  #8 (permalink)  
Antiguo 11/09/2015, 05:42
Avatar de xfxstudios
xfxstudios
 
Fecha de Ingreso: junio-2015
Ubicación: Valencia - Venezuela
Mensajes: 2.448
Antigüedad: 8 años, 11 meses
Puntos: 263
Respuesta: Ejecutar archivos sql desde la web

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
  1. <?php
  2.  
  3. @include_once('phpMyImporter.php');
  4.  
  5. $dbhost = "localhost";
  6. $dbuser = "root";
  7. $dbpass = "";
  8. $dbname = "test";
  9.  
  10. $path      = "http://127.0.0.1/TEST/";
  11. $filename  = $path."test.sql"; // Filename of dump, default: dump.sql
  12. $compress  = false; // Import gz compressed file, default: false
  13.  
  14. $connection = @mysql_connect($dbhost,$dbuser,$dbpass);
  15. $dump = new phpMyImporter($dbname,$connection,$filename,$compress);
  16.  
  17. $dump->utf8 = true; // Uses UTF8 connection with MySQL server, default: true
  18.  
  19. $dump->doImport();
  20. ?>

librearia:
Código PHP:
Ver original
  1. <?php
  2.  
  3. class phpMyImporter {
  4.     /**
  5.     * @Acceso privado
  6.     */
  7.     var $database = null;
  8.     var $connection = null;
  9.     var $compress = null;
  10.     var $utf8 = null;
  11.  
  12.     var $importFilename = null;
  13.  
  14.  
  15.     function phpMyImporter($db=null, $connection=null, $filepath='test.sql', $compress=false) {
  16.         $this->connection = $connection;
  17.         $this->compress = $compress;
  18.         $this->importFilename = $filepath;
  19.  
  20.         $this->utf8 = true;
  21.  
  22.         return $this->setDatabase($db);
  23.     }
  24.  
  25.     /**
  26.     * Sets the database to work on
  27.     * @param string $db The database name
  28.     */
  29.     function setDatabase($db){
  30.         $this->database = $db;
  31.         if ( !@mysql_select_db($this->database) )
  32.             return false;
  33.         return true;
  34.     }
  35.    
  36.     /**
  37.     * Read from SQL file and make sql query
  38.     */
  39.     function importSql($file) {
  40.         // Reading SQL from file
  41.         echo "Leyendo instrucción SQL desde archivo '".$this->importFilename."':<br><br> ";
  42.         if ($this->compress) {
  43.             $lines = gzfile($file);
  44.         }
  45.         else {
  46.             $lines = file($file);
  47.         }
  48.         echo " Finalizada la Lectura!<br><br>\n";
  49.            
  50.         echo "Importando SQL dentro de tu base de datos '".$this->database."':<br><br> ";
  51.         $x = 0;
  52.         $importSql = "";
  53.         $procent = 0;
  54.         foreach ($lines as $line) {
  55.             // Print progress
  56.             $x++;
  57.             $numOfLines = count($lines);
  58.             if ($x%(int)($numOfLines/20) == 0) {
  59.                 $procent += 5;
  60.                 if ($procent%25 == 0) echo "$procent%";
  61.                 else echo ".";
  62.             }
  63.  
  64.             // Importing SQL
  65.             $importSql .= $line;
  66.             if ( substr(trim($line), strlen(trim($line))-1) == ";" ) {
  67.                 $query = @mysql_query($importSql, $this->connection);
  68.                 if (!$query) return false;
  69.                 $importSql = "";
  70.             }
  71.         }
  72.         return true;
  73.     }
  74.    
  75.     /**
  76.     * Import SQL file into selected database
  77.     */
  78.     function doImport() {      
  79.         if ( !$this->setDatabase($this->database) )
  80.             return false;
  81.  
  82.         if ( $this->utf8 ) {
  83.             $encoding = @mysql_query("SET NAMES 'utf8'", $this->connection);
  84.         }
  85.  
  86.         if ( $this->importFilename ) {
  87.             $import = $this->importSql($this->importFilename);
  88.             if (!$import) echo "\n".mysql_error($this->connection)."\n";
  89.             else echo "<br><br> Terminado, Archivo Importado!\n";
  90.             return $import;
  91.         }
  92.         else {
  93.             return false;
  94.         }
  95.     }
  96. }
  97. ?>
__________________
[email protected]
HITCEL