Skip to content

Commit dc3060b

Browse files
mgilfillandshafik
authored andcommitted
Handle custom ports defined in the hostname (#44)
* Handle custom ports defined in the hostname * Improve detection of custom ports when connecting Perstitent connections are defined by prepending the hostname with "p:". This would have been detected as the start of a custom port and broken the connection. A better solution is to detect the port at the end of host name using regex. * Never detect a prefix of "p:" as a hostname This improves edge cases when using a persistent connection with a numeric hostname. For example: "p:253"
1 parent 2236e6f commit dc3060b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lib/mysql.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,17 @@ function mysql_connect(
5959
return \Dshafik\MySQL::$connections[$hash]['conn'];
6060
}
6161

62+
/* A custom port can be specified by appending the hostname with :{port} e.g. hostname:3307 */
63+
if (preg_match('/^(.+):([\d]+)$/', $hostname, $port_matches) === 1 && $port_matches[1] !== "p") {
64+
$hostname = $port_matches[1];
65+
$port = (int) $port_matches[2];
66+
} else {
67+
$port = null;
68+
}
69+
6270
/* No flags, means we can use mysqli_connect() */
6371
if ($flags === 0) {
64-
$conn = mysqli_connect($hostname, $username, $password);
72+
$conn = mysqli_connect($hostname, $username, $password, '', $port);
6573
if (!$conn instanceof mysqli) {
6674
return false;
6775
}
@@ -82,7 +90,7 @@ function mysql_connect(
8290
$username,
8391
$password,
8492
'',
85-
null,
93+
$port,
8694
'',
8795
$flags
8896
);

0 commit comments

Comments
 (0)