Wenn Sie MySQL-Tabellen zur Laufzeit einer PHP-Anwendung importieren möchten, zeige ich Ihnen hier, wie Sie MySQL-Tabellen mit PHP einfach wiederherstellen können. Im Allgemeinen verwenden Sie zum Importieren einer MySQL-Datenbank aus PHPMyAdmin. Dies ist eine der einfachsten Methoden zum Importieren einer MySQL-Datenbank. Wenn Sie jedoch nach einer Lösung zum Importieren einer Datenbank während der Installation einer PHP-Anwendung wie WordPress, Joomla, Drupal usw. suchen, finden Sie unten die einfache PHP-Methode für Importieren der MySQL-Datenbank ohne PHPMyAdmin.

MySql-Tabellen mit PHP importieren
Verwenden Sie das folgende PHP-Skript, um MySQL-Datenbanktabellen zu importieren / wiederherzustellen.
<?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";
?> |