What are common pitfalls when trying to access MySQL database from a PHP file on a web server?

One common pitfall when trying to access a MySQL database from a PHP file on a web server is not properly handling database connection errors. To solve this issue, it is important to include error handling mechanisms to catch and display any potential errors that may occur during the database connection process.

<?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("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>