Se vuoi importare tabelle mysql in fase di esecuzione dell'applicazione php, qui ti mostrerò come ripristinare facilmente le tabelle mysql usando PHP. Generalmente si utilizza per importare il database mysql da PHPMyAdmin, è uno dei metodi più semplici per importare il database mysql ma se stai cercando una soluzione per importare il database durante l'installazione di applicazioni php come wordpress, joomla, drupal ecc, di seguito è riportato il semplice metodo PHP per importazione di database mysql senza PHPMyAdmin.

Importazione di tabelle MySql utilizzando PHP
Usa il seguente script php per importare/ripristinare le tabelle del database mysql.
<?php
// Set database credentials
$hostname = 'localhost'; // MySql Host
$username = 'root'; // MySql Username
$password = 'root'; // MySql Password
$dbname = 'dbname'; // MySql Database Name
// File Path which need to import
$filePath = 'sql_files/mysql_db.sql';
// Connect & select the database
$con = new mysqli($hostname, $username, $password, $dbname);
// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filePath);
$error = '';
// Loop through each line
foreach ($lines as $line){
// Skip it if it's a comment
if(substr($line, 0, 2) == '--' || $line == ''){
continue;
}
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';'){
// Perform the query
if(!$con->query($templine)){
$error .= 'Error performing query "<b>' . $templine . '</b>": ' . $db->error . '<br /><br />';
}
// Reset temp variable to empty
$templine = '';
}
}
$con->close();
echo !empty($error)?$error:"Import Success";
?> |