Skip to content

Commit

Permalink
Add possibility to sepcify port on Mysqli and PDO
Browse files Browse the repository at this point in the history
  • Loading branch information
Typo 13 committed Feb 23, 2021
1 parent aabde76 commit 61bbf5a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
7 changes: 6 additions & 1 deletion StorageAdapter/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class Mysqli implements AdapterInterface
{
protected $database_host;
protected $database_port;
protected $database_user;
protected $database_pass;
protected $database_name;
Expand Down Expand Up @@ -91,7 +92,11 @@ protected function openDatabase()
return false;
}

$this->mysqli_conn = new \mysqli($this->database_host, $this->database_user, $this->database_pass, $this->database_name);
if ($this->database_port) {
$this->mysqli_conn = new \mysqli($this->database_host, $this->database_user, $this->database_pass, $this->database_name, intval($this->database_port, 10));
} else {
$this->mysqli_conn = new \mysqli($this->database_host, $this->database_user, $this->database_pass, $this->database_name);
}

if (mysqli_connect_error()) {
trigger_error("Mysqli database connection failed. Error " . mysqli_connect_errno() . ': ' . mysqli_connect_error(), E_USER_WARNING);
Expand Down
15 changes: 12 additions & 3 deletions StorageAdapter/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class PDO implements AdapterInterface
{
protected $database_driver;
protected $database_host;
protected $database_port;
protected $database_user;
protected $database_pass;
protected $database_name;
Expand Down Expand Up @@ -103,9 +104,17 @@ protected function getDsn()
throw new \Exception('Database adapter "database_user" option cannot be empty');
}

$dsn .= sprintf('host=%s;dbname=%s',
$this->database_host,
$this->database_name);
if ($this->database_port) {
$dsn .= sprintf('host=%s;port=%s;dbname=%s',
$this->database_host,
$this->database_port,
$this->database_name);
} else {
$dsn .= sprintf('host=%s;dbname=%s',
$this->database_host,
$this->database_name);
}

break;
}

Expand Down
2 changes: 2 additions & 0 deletions securimage.php
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ public function __construct($options = array())
$this->securimage_path . '/database/securimage.sq3');
} else {
$dbOpts['database_host'] = @$options['database_host'];
$dbOpts['database_port'] = !empty($options['database_port']) ? $options['database_port'] : null;
$dbOpts['database_name'] = @$options['database_name'];
$dbOpts['database_user'] = @$options['database_user'];
$dbOpts['database_pass'] = @$options['database_pass'];
Expand All @@ -880,6 +881,7 @@ public function __construct($options = array())

$mysqliOpts = array(
'database_host' => $options['database_host'],
'database_port' => !empty($options['database_port']) ? $options['database_port'] : null,
'database_name' => $options['database_name'],
'database_user' => $options['database_user'],
'database_pass' => $options['database_pass'],
Expand Down

0 comments on commit 61bbf5a

Please sign in to comment.