What are some common pitfalls when setting up a PHP script to connect to a MySQL database?

Common pitfalls when setting up a PHP script to connect to a MySQL database include using incorrect database credentials, not enabling the MySQL extension in PHP, and not handling errors properly. To solve these issues, make sure to double-check the database host, username, password, and database name, enable the MySQL extension in your PHP configuration, and use error handling to catch any connection errors.

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$database = "my_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>