Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PHP error from a failed database connection #7815

Merged

Conversation

jdarwood007
Copy link
Member

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.

@albertlast I don't have PG setup anymore to test. But I believe it will suffer the same problem according to the PHP docs. Have you seen that?

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.
@jdarwood007 jdarwood007 added this to the 2.1.5 milestone Jul 31, 2023
@albertlast
Copy link
Collaborator

when i turn off my pg instance i get this error message:

grafik
php 8.1.1

@sbulen
Copy link
Contributor

sbulen commented Jul 31, 2023

I am not sure this helps anything?

If db_connection is null that means the connection failed (or was somehow bypassed, which shouldn't happen).

If we return an empty string, I believe all we will see in these circumstances is a blank error message in smf_error_log. I think that is actually less helpful than 'argument is of type null'.

It would be more helpful to find out why the db connection was lost, if available. Or put a proper "db connection lost" error of some type.

@albertlast
Copy link
Collaborator

updated env to 8.2.8 same behavior like 8.1.1 mention before

@jdarwood007
Copy link
Member Author

I am seeing the fatal php error in a MySQL 8.0 and PHP 8.1 env.
The error isn't being displayed to users, but PHP is logging that fatal error.

@jdarwood007
Copy link
Member Author

Here is the full PHP error given.

PHP Fatal error:  Uncaught TypeError: mysqli_error(): Argument #1 ($mysql) must be of type mysqli, null given in /Sources/Errors.php:457
Stack trace:
#0 /Sources/Errors.php(457): mysqli_error(NULL)
#1 /Sources/Subs-Db-mysql.php(101): display_db_error()
#2 /Sources/Load.php(3706): smf_db_initiate('**********', '**********', '*********', '*********', '**********', Array)
#3 /SSI.php(92): loadDatabase()

We could also fix this in Errrors.php by checking that we are not about to use a null, but the wrapper returns a empty string. Which Errors.php will check for. Because its a loose type check, our code would basically make null == ''. So no changes here except we handle the error gracefully.

@jdarwood007
Copy link
Member Author

@live627
Copy link
Contributor

live627 commented Aug 11, 2023

env
Screenshot 2023-08-11 at 15-56-32 Support and Credits

before fix
Screenshot 2023-08-11 at 16-23-06 Screenshot

after fix
Screenshot 2023-08-11 at 16-29-01 Connection Problems

{
global $db_connection;

if (is_null($connection) && is_null($db_connection))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (is_null($connection) && is_null($db_connection))
if (is_null($connection) || is_null($db_connection))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do I need to make the changes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; thinking this over again, it is correct imo because it should check the provided connection before the global one. Also could we explicitly check? Another stupid suggestion, but matches everywhere else; also, I dislike superfluous function calls.

Suggested change
if (is_null($connection) && is_null($db_connection))
if ($connection !== null || $db_connection !== null)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it, but it needs to be and, not or. If either one is not null, it has a db connection and would just return a empty string if no error has occurred.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh oh did I really get those backwards? 😮 I accidentally inverted your logic, sorryyy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It happens to the best of us.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like the inverted logic is in the commit

@jdarwood007
Copy link
Member Author

env Screenshot 2023-08-11 at 15-56-32 Support and Credits

before fix Screenshot 2023-08-11 at 16-23-06 Screenshot

after fix Screenshot 2023-08-11 at 16-29-01 Connection Problems

Basically what I am seeing in the logs. Luckily nothing is being sent to output, because if it was, the PHP error would be exposing the mysql credentials.

@live627
Copy link
Contributor

live627 commented Aug 14, 2023

@sbulen is the provided proof satisfactory

Copy link
Member

@Sesquipedalian Sesquipedalian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c98729c should be reverted.

@jdarwood007
Copy link
Member Author

Inverted the logic. I don't care whether is_null is used or === null. I doubt any speed differences can be found.

@Sesquipedalian Sesquipedalian merged commit 5d05c65 into SimpleMachines:release-2.1 Aug 18, 2023
8 checks passed
@jdarwood007 jdarwood007 deleted the dbErrorNullConnection branch September 23, 2023 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants