skip to Main Content

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.

2

Answers


  1. 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);
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search