From 7c819bfc24aeae807a182aa1b71b80931e4b3d9b Mon Sep 17 00:00:00 2001 From: Sergey Tsalkov Date: Sat, 14 Jun 2014 01:58:51 +0000 Subject: [PATCH] the connect_options variable lets you set mysqli_options, like a connection timeout --- db.class.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/db.class.php b/db.class.php index 0f25bc7..808037d 100644 --- a/db.class.php +++ b/db.class.php @@ -37,6 +37,7 @@ class DB { public static $nested_transactions = false; public static $usenull = true; public static $ssl = array('key' => '', 'cert' => '', 'ca_cert' => '', 'ca_path' => '', 'cipher' => ''); + public static $connect_options = array(MYSQLI_OPT_CONNECT_TIMEOUT => 30); // internal protected static $mdb = null; @@ -49,7 +50,7 @@ class DB { } static $variables_to_sync = array('param_char', 'named_param_seperator', 'success_handler', 'error_handler', 'throw_exception_on_error', - 'nonsql_error_handler', 'throw_exception_on_nonsql_error', 'nested_transactions', 'usenull', 'ssl'); + 'nonsql_error_handler', 'throw_exception_on_nonsql_error', 'nested_transactions', 'usenull', 'ssl', 'connect_options'); $db_class_vars = get_class_vars('DB'); // the DB::$$var syntax only works in 5.3+ @@ -131,6 +132,7 @@ class MeekroDB { public $nested_transactions = false; public $usenull = true; public $ssl = array('key' => '', 'cert' => '', 'ca_cert' => '', 'ca_path' => '', 'cipher' => ''); + public $connect_options = array(MYSQLI_OPT_CONNECT_TIMEOUT => 30); // internal public $internal_mysql = null; @@ -165,13 +167,18 @@ class MeekroDB { if (! $this->port) $this->port = ini_get('mysqli.default_port'); $this->current_db = $this->dbName; $mysql = new mysqli(); + $connect_flags = 0; if ($this->ssl['key']) { $mysql->ssl_set($this->ssl['key'], $this->ssl['cert'], $this->ssl['ca_cert'], $this->ssl['ca_path'], $this->ssl['cipher']); $connect_flags |= MYSQLI_CLIENT_SSL; } - $mysql->real_connect($this->host, $this->user, $this->password, $this->dbName, $this->port, null, $connect_flags); + foreach ($this->connect_options as $key => $value) { + $mysql->options($key, $value); + } + // suppress warnings, since we will check connect_error anyway + @$mysql->real_connect($this->host, $this->user, $this->password, $this->dbName, $this->port, null, $connect_flags); if ($mysql->connect_error) { $this->nonSQLError('Unable to connect to MySQL server! Error: ' . $mysql->connect_error);