How can the use of ports in PHP MySQL connections impact the ability to establish a successful connection?

Using the wrong port number in PHP MySQL connections can prevent the successful establishment of a connection. It is essential to ensure that the correct port number is specified in the connection settings to connect to the MySQL database successfully.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "database";
$port = 3306; // specify the correct port number

// 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";
?>