Skip to content

Commit

Permalink
Merge pull request #6763 from rxu/ticket/17455
Browse files Browse the repository at this point in the history
[ticket/17455] Fix PHP warning on MySQLi connection failure
  • Loading branch information
marc1706 authored Jan 6, 2025
2 parents 4386d1a + 1a14344 commit c0a1214
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions db/driver/mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@ function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = fals
}
}

$this->db_connect_id = mysqli_init();
if (!$this->db_connect_id = mysqli_init())
{
$this->connect_error = 'Failed to initialize MySQLi object.';

if (!@mysqli_real_connect($this->db_connect_id, $this->server, $this->user, $sqlpassword, $this->dbname, $port, $socket, MYSQLI_CLIENT_FOUND_ROWS))
}
else if (!@mysqli_real_connect($this->db_connect_id, $this->server, $this->user, $sqlpassword, $this->dbname, $port, $socket, MYSQLI_CLIENT_FOUND_ROWS))
{
$this->db_connect_id = '';
$this->connect_error = 'Failed to establish a connection to the MySQL database engine. Please ensure MySQL server is running and the database configuration parameters are correct.';
}

if ($this->db_connect_id && $this->dbname != '')
if (!$this->connect_error && $this->db_connect_id && $this->dbname != '')
{
// Disable loading local files on client side
@mysqli_options($this->db_connect_id, MYSQLI_OPT_LOCAL_INFILE, false);
Expand Down Expand Up @@ -357,15 +360,8 @@ function _sql_error()
if ($this->db_connect_id)
{
$error = [
'message' => $this->db_connect_id->error,
'code' => $this->db_connect_id->errno,
];
}
else if (function_exists('mysqli_connect_error'))
{
$error = [
'message' => $this->db_connect_id->connect_error,
'code' => $this->db_connect_id->connect_errno,
'message' => $this->db_connect_id->connect_error ?: $this->db_connect_id->error,
'code' => $this->db_connect_id->connect_errno ?: $this->db_connect_id->errno,
];
}
else
Expand Down

0 comments on commit c0a1214

Please sign in to comment.