Skip to content

Commit 958edbe

Browse files
PDO - support username & password specified in DSN
1 parent e0c4c39 commit 958edbe

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

ext/pdo_mysql/mysql_driver.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,11 @@ static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
549549
struct pdo_data_src_parser vars[] = {
550550
{ "charset", NULL, 0 },
551551
{ "dbname", "", 0 },
552-
{ "host", "localhost", 0 },
553-
{ "port", "3306", 0 },
552+
{ "host", "localhost", 0 },
553+
{ "port", "3306", 0 },
554554
{ "unix_socket", PDO_DEFAULT_MYSQL_UNIX_ADDR, 0 },
555+
{ "user", NULL, 0 },
556+
{ "password", NULL, 0 },
555557
};
556558
int connect_opts = 0
557559
#ifdef CLIENT_MULTI_RESULTS
@@ -577,7 +579,7 @@ static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
577579
PDO_DBG_INF("multi results");
578580
#endif
579581

580-
php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 5);
582+
php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 7);
581583

582584
H = pecalloc(1, sizeof(pdo_mysql_db_handle), dbh->is_persistent);
583585

@@ -775,6 +777,14 @@ static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
775777
unix_socket = vars[4].optval;
776778
}
777779

780+
if (!dbh->username && vars[5].optval) {
781+
dbh->username = vars[5].optval;
782+
}
783+
784+
if (!dbh->password && vars[6].optval) {
785+
dbh->password = vars[6].optval;
786+
}
787+
778788
/* TODO: - Check zval cache + ZTS */
779789
#ifdef PDO_USE_MYSQLND
780790
if (dbname) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
PDO_MYSQL: Pass credentials in dsn instead of constructor params
3+
--SKIPIF--
4+
<?php
5+
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
6+
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7+
MySQLPDOTest::skip();
8+
?>
9+
--FILE--
10+
<?php
11+
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
12+
13+
class MySQLPDOTestDiscardCredentialParams extends MySQLPDOTest {
14+
public function __construct($dsn, $user, $pass, $attr) {
15+
return parent::__construct($dsn, null, null, $attr);
16+
}
17+
}
18+
19+
$link = MySQLPDOTestDiscardCredentialParams::factory('PDO', false, null, ['user' => PDO_MYSQL_TEST_USER, 'password' => PDO_MYSQL_TEST_PASS]);
20+
echo "using credentials in dsn: done\n";
21+
22+
// test b/c - credentials in DSN are ignored when user/pass passed as separate params
23+
$link = MySQLPDOTest::factory('PDO', false, null, ['user' => 'incorrect', 'password' => 'ignored']);
24+
echo "ignoring credentials in dsn: done\n";
25+
?>
26+
--EXPECTF--
27+
done!

0 commit comments

Comments
 (0)