可以强制 mysqli 函数返回错误代码而不是抛出异常吗?

huangapple go评论64阅读模式
英文:

Can I force mysqli functions to return error codes instead of throwing exceptions?

问题

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.

英文:

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.

答案1

得分: 3

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.

英文:

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.

答案2

得分: 2

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);

英文:

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);

huangapple
  • 本文由 发表于 2023年5月14日 15:13:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定