Skip to content

Commit 4116174

Browse files
authored
Merge pull request #6 from byjg/issue-5
Issue 5
2 parents e329156 + f920454 commit 4116174

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

docs/Connecting-to-MySQL-via-SSL.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Connecting To MySQL via SSL
2+
3+
(Read here https://gist.github.com/byjg/860065a828150caf29c20209ecbd5692 about create server mysql)
4+
5+
```php
6+
<?php
7+
$sslCa = "/path/to/ca";
8+
$sslKey = "/path/to/Key";
9+
$sslCert = "/path/to/cert";
10+
$sslCaPath = "/path";
11+
$sslCipher = "DHE-RSA-AES256-SHA:AES128-SHA";
12+
$verifySsl = 'false'; // or 'true'. Since PHP 7.1.
13+
14+
$db = \ByJG\AnyDataset\Factory::getDbRelationalInstance(
15+
"mysql://localhost/database?ca=$sslCa&key=$sslKey&cert=$sslCert&capath=$sslCaPath&verifyssl=$verifySsl&cipher=$sslCipher"
16+
);
17+
18+
$iterator = $db->getIterator('select * from table where field = :value', ['value' => 10]);
19+
foreach ($iterator as $row) {
20+
// Do Something
21+
// $row->getField('field');
22+
}

example.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
require "vendor/autoload.php";
33

4-
$db = new \ByJG\AnyDataset\Repository\DBDataset('mysql://root:aaaaaaa@10.10.10.101/development');
4+
$db = \ByJG\AnyDataset\Factory::getDbRelationalInstance('mysql://root:aaaaaaa@localhost/development');
55

66
$iterator = $db->getIterator('select * from airports where idairports = [[idairports]]', ['idairports' => 898]);
77

src/Store/PdoMysql.php

+24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
class PdoMysql extends DbPdoDriver
99
{
1010

11+
protected $mysqlAttr = [
12+
"ca" => PDO::MYSQL_ATTR_SSL_CA,
13+
"capath" => PDO::MYSQL_ATTR_SSL_CAPATH,
14+
"cert" => PDO::MYSQL_ATTR_SSL_CERT,
15+
"key" => PDO::MYSQL_ATTR_SSL_KEY,
16+
"cipher" => PDO::MYSQL_ATTR_SSL_CIPHER,
17+
"verifyssl" => 1014 // PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT (>=7.1)
18+
];
19+
1120
/**
1221
* PdoMysql constructor.
1322
*
@@ -25,6 +34,21 @@ public function __construct(Uri $connUri)
2534
PDO::ATTR_EMULATE_PREPARES => true
2635
];
2736

37+
if (!empty($connUri->getQuery())) {
38+
foreach ($this->mysqlAttr as $key => $property) {
39+
$value = $connUri->getQueryPart($key);
40+
if (!empty($value)) {
41+
$prepValue = urldecode($value);
42+
if ($prepValue === 'false') {
43+
$prepValue = false;
44+
} else if ($prepValue === 'true') {
45+
$prepValue = true;
46+
}
47+
$preOptions[$property] = $prepValue;
48+
}
49+
}
50+
}
51+
2852
$this->setSupportMultRowset(true);
2953

3054
parent::__construct($connUri, $preOptions, $postOptions);

0 commit comments

Comments
 (0)