What role does phpMyAdmin play in importing databases in PHP?
phpMyAdmin is a web-based tool that allows users to manage MySQL databases. To import a database using phpMyAdmin, users can simply navigate to the Import tab, choose the SQL file containing the database they want to import, and then click Go. This process is straightforward and does not require any coding. However, if you want to automate the process of importing databases in PHP, you can use the following code snippet:
// Set up database connection
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL file containing the database to import
$sql_file = "path/to/database.sql";
// Read SQL file
$sql = file_get_contents($sql_file);
// Execute SQL commands
if ($conn->multi_query($sql) === TRUE) {
echo "Database imported successfully";
} else {
echo "Error importing database: " . $conn->error;
}
// Close connection
$conn->close();