diff options
author | Mike Gilfillan <mike@edgeoftheweb.co.uk> | 2019-08-07 00:47:07 +0100 |
---|---|---|
committer | Davey Shafik <me@daveyshafik.com> | 2019-08-06 16:47:07 -0700 |
commit | dc3060b802bd1c8e819efe0236813d67375fba90 (patch) | |
tree | df70a50f02009e32314e670185451a41abe42726 /lib/mysql.php | |
parent | 2236e6f4cd5fec4439cba781bc938301b7caaddc (diff) | |
download | php7-mysql-shim-dc3060b802bd1c8e819efe0236813d67375fba90.zip php7-mysql-shim-dc3060b802bd1c8e819efe0236813d67375fba90.tar.gz php7-mysql-shim-dc3060b802bd1c8e819efe0236813d67375fba90.tar.bz2 |
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"
Diffstat (limited to 'lib/mysql.php')
-rw-r--r-- | lib/mysql.php | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/lib/mysql.php b/lib/mysql.php index 7b0201d..732c43a 100644 --- a/lib/mysql.php +++ b/lib/mysql.php @@ -59,9 +59,17 @@ namespace { return \Dshafik\MySQL::$connections[$hash]['conn']; } + /* A custom port can be specified by appending the hostname with :{port} e.g. hostname:3307 */ + if (preg_match('/^(.+):([\d]+)$/', $hostname, $port_matches) === 1 && $port_matches[1] !== "p") { + $hostname = $port_matches[1]; + $port = (int) $port_matches[2]; + } else { + $port = null; + } + /* No flags, means we can use mysqli_connect() */ if ($flags === 0) { - $conn = mysqli_connect($hostname, $username, $password); + $conn = mysqli_connect($hostname, $username, $password, '', $port); if (!$conn instanceof mysqli) { return false; } @@ -82,7 +90,7 @@ namespace { $username, $password, '', - null, + $port, '', $flags ); |