What is the common error message "Can't connect to local MySQL server through socket" in PHP and how can it be resolved?

The error message "Can't connect to local MySQL server through socket" in PHP usually occurs when the MySQL server is not running or the socket file specified in the connection settings is incorrect. To resolve this issue, you can try starting the MySQL server, checking the socket file path in your PHP code, or specifying the host and port in the connection settings.

// Example PHP code snippet to connect to MySQL server using host and port
$servername = "localhost";
$username = "username";
$password = "password";
$database = "dbname";
$port = "3306"; // Default MySQL port

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

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