What are common reasons for a "Verbindung fehlgeschlagen" error when trying to connect to a MySQL database using PHP?
The "Verbindung fehlgeschlagen" error typically occurs when there is an issue with the connection to the MySQL database, such as incorrect credentials, server unavailability, or firewall restrictions. To solve this issue, double-check the database credentials, ensure the MySQL server is running and accessible, and verify that there are no firewall restrictions blocking the connection.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Verbindung fehlgeschlagen: " . $conn->connect_error);
}
echo "Verbindung erfolgreich hergestellt";
?>