From 40d4cd39c183cf65cf059d06a8514429fb60b8ca Mon Sep 17 00:00:00 2001 From: jdarwood007 Date: Mon, 31 Jul 2023 08:00:07 -0700 Subject: [PATCH 1/3] Fix PHP error from a failed database connection PHP 8.1+ will generate the following error upon a failed database connection: ``` PHP Fatal error: Uncaught TypeError: mysqli_error(): Argument #1 ($mysql) must be of type mysqli, null given in /Sources/Errors.php:464 ``` To fix this, I built a wrapper to check to ensure that the we don't have a null argument going into the related function. Instead if its null and the primary connection is null, it returns a empty string, which is what the code seems to expect. --- Sources/Subs-Db-mysql.php | 18 +++++++++++++++++- Sources/Subs-Db-postgresql.php | 18 +++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Sources/Subs-Db-mysql.php b/Sources/Subs-Db-mysql.php index cf341008d5..ed9ec22477 100644 --- a/Sources/Subs-Db-mysql.php +++ b/Sources/Subs-Db-mysql.php @@ -50,7 +50,7 @@ function smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, 'db_server_info' => 'smf_db_get_server_info', 'db_affected_rows' => 'smf_db_affected_rows', 'db_transaction' => 'smf_db_transaction', - 'db_error' => 'mysqli_error', + 'db_error' => 'smf_db_errormsg', 'db_select_db' => 'smf_db_select', 'db_title' => MYSQL_TITLE, 'db_sybase' => false, @@ -1099,4 +1099,20 @@ function smf_db_escape_string($string, $connection = null) return mysqli_real_escape_string($connection === null ? $db_connection : $connection, $string); } +/** + * Wrapper to handle null errors + * + * @param null|mysqli $connection = null The connection to use (null to use $db_connection) + * @return string escaped string + */ +function smf_db_errormsg($connection = null) +{ + global $db_connection; + + if (is_null($connection) && is_null($db_connection)) + return ''; + + return mysqli_error($connection === null ? $db_connection : $connection); +} + ?> \ No newline at end of file diff --git a/Sources/Subs-Db-postgresql.php b/Sources/Subs-Db-postgresql.php index 27bcc4c41d..cef9e40803 100644 --- a/Sources/Subs-Db-postgresql.php +++ b/Sources/Subs-Db-postgresql.php @@ -52,7 +52,7 @@ function smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, &$db_prefix 'db_server_info' => 'smf_db_version', 'db_affected_rows' => 'smf_db_affected_rows', 'db_transaction' => 'smf_db_transaction', - 'db_error' => 'pg_last_error', + 'db_error' => 'smf_db_errormsg', 'db_select_db' => 'smf_db_select_db', 'db_title' => POSTGRE_TITLE, 'db_sybase' => true, @@ -1033,4 +1033,20 @@ function smf_db_connect_errno() return $pg_connect_errno; } +/** + * Wrapper to handle null errors + * + * @param null|PgSql\Connection $connection = null The connection to use (null to use $db_connection) + * @return string escaped string + */ +function smf_db_errormsg($connection = null) +{ + global $db_connection; + + if (is_null($connection) && is_null($db_connection)) + return ''; + + return pg_last_error($connection === null ? $db_connection : $connection); +} + ?> \ No newline at end of file From c98729c857f0004e8b36dacace7bcd728e1330c0 Mon Sep 17 00:00:00 2001 From: jdarwood007 Date: Mon, 14 Aug 2023 06:59:21 -0700 Subject: [PATCH 2/3] check check --- Sources/Subs-Db-mysql.php | 2 +- Sources/Subs-Db-postgresql.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Subs-Db-mysql.php b/Sources/Subs-Db-mysql.php index ed9ec22477..ea37827009 100644 --- a/Sources/Subs-Db-mysql.php +++ b/Sources/Subs-Db-mysql.php @@ -1109,7 +1109,7 @@ function smf_db_errormsg($connection = null) { global $db_connection; - if (is_null($connection) && is_null($db_connection)) + if ($connection !== null && $db_connection !== null) return ''; return mysqli_error($connection === null ? $db_connection : $connection); diff --git a/Sources/Subs-Db-postgresql.php b/Sources/Subs-Db-postgresql.php index cef9e40803..231f150d92 100644 --- a/Sources/Subs-Db-postgresql.php +++ b/Sources/Subs-Db-postgresql.php @@ -1043,7 +1043,7 @@ function smf_db_errormsg($connection = null) { global $db_connection; - if (is_null($connection) && is_null($db_connection)) + if ($connection !== null && $db_connection !== null) return ''; return pg_last_error($connection === null ? $db_connection : $connection); From baf405021c556c4cc490668e43b08214fbc53ef1 Mon Sep 17 00:00:00 2001 From: jdarwood007 Date: Thu, 17 Aug 2023 17:53:11 -0700 Subject: [PATCH 3/3] Inverted logic --- Sources/Subs-Db-mysql.php | 2 +- Sources/Subs-Db-postgresql.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Subs-Db-mysql.php b/Sources/Subs-Db-mysql.php index ea37827009..fa6403d9aa 100644 --- a/Sources/Subs-Db-mysql.php +++ b/Sources/Subs-Db-mysql.php @@ -1109,7 +1109,7 @@ function smf_db_errormsg($connection = null) { global $db_connection; - if ($connection !== null && $db_connection !== null) + if ($connection === null && $db_connection === null) return ''; return mysqli_error($connection === null ? $db_connection : $connection); diff --git a/Sources/Subs-Db-postgresql.php b/Sources/Subs-Db-postgresql.php index 231f150d92..84e3feeada 100644 --- a/Sources/Subs-Db-postgresql.php +++ b/Sources/Subs-Db-postgresql.php @@ -1043,7 +1043,7 @@ function smf_db_errormsg($connection = null) { global $db_connection; - if ($connection !== null && $db_connection !== null) + if ($connection === null && $db_connection === null) return ''; return pg_last_error($connection === null ? $db_connection : $connection);