diff options
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | lib/mysql.php | 9 | ||||
-rw-r--r-- | tests/MySqlShimTest.php | 13 |
3 files changed, 25 insertions, 1 deletions
@@ -26,5 +26,7 @@ Once the file is included, it will create `mysql_*` function if they don't alrea - Calls to `is_resource()` and `get_resource_type()` on MySQL connections and results will fail as these are now their `mysqli` equivalents. - Some errors are now from `ext/mysqli`, and others are `E_USER_WARNING` instead of `E_WARNING`. -- You must use the `mysqli.*` INI entries instead of `mysql.*` (e.g. `mysqli.default_user` instead of `mysql.default_usert`) +- You must use the `mysqli.*` INI entries instead of `mysql.*` (e.g. `mysqli.default_user` instead of `mysql.default_user`) - Column lengths reported by `mysql_field_len()` assume `latin1` +- If no host, username, password parameter is provided when using the `mysql_*` functions, the default values from the corresponding `mysqli.*` settings from `php.ini` file will be used +(e.g. `mysqli.default_host`, `mysqli.default_user`, `mysqli.default_pw`) diff --git a/lib/mysql.php b/lib/mysql.php index 9fa32cb..e587c2a 100644 --- a/lib/mysql.php +++ b/lib/mysql.php @@ -20,6 +20,15 @@ namespace { if ($new !== false) { trigger_error('Argument $new is no longer supported in PHP > 7', E_USER_WARNING); } + if (null === $hostname) { + $hostname = ini_get('mysqli.default_host') ?: null; + } + if (null === $username) { + $username = ini_get('mysqli.default_user') ?: null; + } + if (null === $password) { + $password = ini_get('mysqli.default_pw') ?: null; + } $hash = sha1($hostname . $username . $flags); if ($hostname{1} != ':' && isset(\Dshafik\MySQL::$connections[$hash])) { diff --git a/tests/MySqlShimTest.php b/tests/MySqlShimTest.php index 2945464..ccaeec8 100644 --- a/tests/MySqlShimTest.php +++ b/tests/MySqlShimTest.php @@ -24,6 +24,19 @@ class MySqlShimTest extends \PHPUnit_Framework_TestCase } /** + * @requires PHP 7 + */ + public function test_mysql_connect_defaults() + { + ini_set('mysqli.default_host', '127.0.0.1'); + ini_set('mysqli.default_user', 'root'); + ini_set('mysqli.default_pw', ''); + + $mysql = mysql_connect(); + $this->assertConnection($mysql); + } + + /** * @expectedException \PHPUnit_Framework_Error_Warning * @expectedExceptionMessageRegExp /^mysql(i?)_connect\(\): (\(HY000\/1045\): )?Access denied for user 'baduser'@'(.*?)' \(using password: YES\)$/ */ |