Phpagi mysql_error() function issue with PHP 8

I managed to download and include the phpagi.php which is comaptible with PHP version 8 on my Asterisk 18 from here.

However, mysql_error() functions wherever called within are failing…

I checked the phpagi.php file and it says:

  // removing the mysql_error() function from the error message due to it being removed in PHP 8+
        if(strpos(' '.strtolower($message), 'mysql')) {
            if(function_exists('mysqli_errno')) {
                $message .= 'MySQL error ' . mysqli_errno() . ": " . mysqli_error() . "\n\n";
            }
        }

but my AGI script which is running on PHP 5.2 on Asterisk 1.8 has mysql_error () function called in several times to check ping and connectivity…one example is:

if (!$this->_sqllink || mysqli_errno($this->_sqllink) == 2006) {
            $agi->verbose("SQL link is invalid or server has gone away", 1);
             if (!mysqli_ping($this->_sqllink)) {  
                $agi->verbose("Reconnecting to the database", 1);
                 mysqli_close($this->_sqllink);
                 $this->sqlConnect();
             }
         }   

but this now breaks my AGI as soon as the ‘if’ statement is hit.

Any leads on how to include SQL checks without breaking the AGI as well as something that is compatible with PHP 8+ or a mysql_error function replacement?

Thanks!
Hisham

There are multiple if statements in the code you posted - which one is breaking ? What do your logs say in Asterisk and in MySQL ?

When it throws the error, what does the error say? Also, keep in mind that if you try to use a non-existent function in PHP or any other programming language, it will fail. If this is the case, you will need to update the code to remove or change the function to a compatible one.

This is what the phpagi.php file says:

// removing the mysql_error() function from the error message due to it being removed in PHP 8+
        if(strpos(' '.strtolower($message), 'mysql')) {
            if(function_exists('mysqli_errno')) {
                $message .= 'MySQL error ' . mysqli_errno() . ": " . mysqli_error() . "\n\n";
            }
        }

I guess the commneted line on top of this function is the reason i am unable to use it in my case…
As soon as i am supposed to hit the mysqli_errno function, the agi script exits abruptly…

So far you just showed pieces of your php code, that doesn’t help much, we need to know what error it shows when your AGI script runs and breaks; Check your php.ini file where your logs are stored, generally they are stored on syslogs, so check /var/log/syslog and let us know what ERROR messages show up when you run your AGI code and it crashes.

If it’s a regular AGI (i.e. not FastAGI), then presumably it will only run for a short while on each invocation—not long enough to worry about the MySQL link going away and having to reconnect. So perhaps simpler code, without all that reconnection rigmarole, would be better.

Also, while you’re at it, I would recommend putting in something like this at the top, if it’s not already the default:

error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

That way, you get errors being reported directly in your logs, instead of your code mysteriously misbehaving or dying without any messages.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.