What are common reasons for a mysqli error when connecting to a database using OOP in PHP?
Common reasons for a mysqli error when connecting to a database using OOP in PHP include incorrect database credentials, server connection issues, or improper handling of errors. To solve this issue, double-check your database credentials, ensure the server is running, and use error handling to catch any potential issues.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>