3

I maintain a website, written in PHP a while back. Most of its functions rely on returning an error, instead of catching exceptions. Something like this:

(Note: something that was a perfectly accepted behavior in prior versions of PHP.)

$tbl_nm = "table_name";
$link = @mysqli_connect($HOSTNAME, $USERNAME, $PASSWD);
if($link !== false)
{
 if(@mysqli_select_db($link, $DBNAME))
 {
 $query = "CREATE TABLE `".$tbl_nm."` (".
 $COL_ID." INT AUTO_INCREMENT PRIMARY KEY, ".
 $COL_DATE." DATE, ".
 $COL_TIME." TIME, ".
 $COL_MSG." TINYTEXT".
 ")";
 
 $res = @mysqli_query($link, $query); //** EXCEPTION RAISED HERE! **
 if($res === false)
 {
 if(mysqli_errno($link) != 1050) //1050 - if table already exists
 {
 logMySQLErrorReport($link, "Error creating table");
 }
 }
 //....
 }
 else
 {
 logMySQLErrorReport($link, "Error setting db");
 }
}
else
{
 logMySQLErrorReport($link, "Error connecting");
}

Now, the shared host forced us to upgrade it to PHP 8.2. As a result the line that calls @mysqli_query instead of returning an error throws an exception:

PHP Fatal error: Uncaught mysqli_sql_exception: Table 'table_name' already exists

and the website doesn't load.

Is there a setting to force it to return an error code instead of throwing an exception?

PS. There's a lot of code like I showed above. I do not want to waste weeks re-writing it with the exception handling, as that language/PHP is pretty much dead to me (because of this exact behavior of its maintainers - when they introduce a radical change to something that was accepted just a few versions back.) I will never code anything in PHP ever again. I just want to make this site work without spending a month refactoring what was a perfectly working code.

Dharman
34k27 gold badges105 silver badges156 bronze badges
asked May 14, 2023 at 7:13
3
  • 2
    Do I get it right that this code tries to create a table every single time it's called, fails, and moves on? Commented May 14, 2023 at 7:23
  • 4
    Truth to be told, most of this useless error handling code doesn't necessarily need to be re-written. It can be just left as a deadweight, because most exceptions don't really need any "handling" here. The only problem is this not-overly-sensible table creation algorithm. May I suggest adding IF NOT EXISTS clause to the CREATE TABLE query? It will tell mysql not to raise the error and your website will load all right Commented May 14, 2023 at 7:56
  • If you do not maintain this project anymore then don't upgrade PHP version. Commented May 14, 2023 at 10:29

3 Answers 3

8

Yes, you still can force mysqli to not report any errors. This hasn't changed over the years. The only thing that changed is the default setting. Previously error reporting was switched off by default and since PHP 8.1 it is enabled.

It can be disabled by putting this line before you open mysqli connection:

mysqli_report(MYSQLI_REPORT_OFF);

But please DO NOT SILENCE ERROR REPORTING! You are supposed to fix errors, not silence them. In your case, you can easily fix the error by adding "IF NOT EXISTS" to your SQL, although I don't understand why your code is trying to create a table in the first place.

answered May 14, 2023 at 10:50
Sign up to request clarification or add additional context in comments.

Comments

5

The mysqli_query function (https://www.php.net/manual/en/mysqli.query.php) is no longer returning false when the table does not exist. In PHP 8.0.16, false is returned, but in PHP 8.1.0 and PHP 8.1.3, an exception is raised by default. add this to your php

mysqli_report(MYSQLI_REPORT_OFF);
Your Common Sense
158k42 gold badges226 silver badges374 bronze badges
answered May 14, 2023 at 8:00

1 Comment

2

If you still want the warnings but don't want the exceptions use this instead:

mysqli_report(MYSQLI_REPORT_ALL ^ MYSQLI_REPORT_STRICT);

answered Apr 3, 2024 at 12:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.