What mysqli works with PHP8 and mariadb?

I've been trying for some time to get PHP8 "talking" to a mariqdb database using mysqli, but it appears to me the mysqli may be broken.

I have installed the following packages:

mariadb-server
mariadb-client
php8-mod-mbstring
php8-cgi
php8-cli
php8-mod-myslqi

Both mysql and PHP8 work fine individually from the command line, and PHP works fine through the uhttpd server. When I run "php -m | grep mysqli", it does show mysqli is present.

But when running a PHP script that attempts to connect to the mysql server. It fails with the following error:

root@FriendlyWrt:~# php-cli -f NewEvent.php
Fatal error: Uncaught mysqli_sql_exception: No such file or directory in /root/CommonDefs.php:64
Stack trace:
#0 /root/CommonDefs.php(64): mysqli->__construct('localhost', 'pi', 'Raspberry', 'qdtimerdata')
#1 /root/NewEvent.php(22): connect()
#2 {main}
  thrown in /root/CommonDefs.php on line 64
root@FriendlyWrt:~#

It basically fails almost immediately as soon as it tries to connect to the mysql server. Here is the pertinent code:

define('DB_HOST', 		'localhost');	// Server Name
define('DB_USER', 		'pi'); 					// Username
define('DB_PASS', 		'Raspberry');			// Password
define('DB_NAME', 		'qdtimerdata');			// Database Name

$conn = connect();      // This is the first executable line of the script, and it fails
...

function connect()
{
	// Connect to database
	$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);   <<<=========  fails here
	$conn OR fail("Connect failed", CONNECT_FAIL);
	return $conn;
}

Am I using the right mysqli? This is the only one I can find for OpenWRT on the NanoPi-R5C I'm running.

Where did you get the openwrt image, afaik the R5C isn't supported here...

The OS image came from the manufacturer, "Friendly". It all works just fine. My only problem is PHP not talking to SQL. I've tried both php8-mod-mysqli and php8-mod-mysqlnd, both show in phpinfo() as being installed, but both throw the following error when attempting to create the mysqli object:

root@FriendlyWrt:~# php-cli -f NewEvent.php
Fatal error: Uncaught Error: Class "mysqli" not found in /root/CommonDefs.php:64
Stack trace:
#0 /root/NewEvent.php(29): connect()
#1 {main}
  thrown in /root/CommonDefs.php on line 64
root@FriendlyWrt:~#

This seems to be a very common problem, but I've found no way to solve it. PHP says the library is available, it has the correct path and file name, so why doesn't it work?

It appears you are using firmware that is not from the official OpenWrt project.

When using forks/offshoots/vendor-specific builds that are "based on OpenWrt", there may be many differences compared to the official versions (hosted by OpenWrt.org). Some of these customizations may fundamentally change the way that OpenWrt works. You might need help from people with specific/specialized knowledge about the firmware you are using, so it is possible that advice you get here may not be useful.

You may find that the best options are:

Install an official version of OpenWrt, if your device is supported (see https://firmware-selector.openwrt.org).
Ask for help from the maintainer(s) or user community of the specific firmware that you are using.
Provide the source code for the firmware so that users on this forum can understand how your firmware works (OpenWrt forum users are volunteers, so somebody might look at the code if they have time and are interested in your issue).
If you believe that this specific issue is common to generic/official OpenWrt and/or the maintainers of your build have indicated as such, please feel free to clarify.

I'm probably most interested in finding someone who HAS gotten this combination to work properly, or is more familiar with PHP configuration. I assume it is something stupid I am doing, or some config file setting I am missing. I am far from expert in this area, but the code I'm using has worked perffectly for years on a Raspberry Pi3B with Raspbian, so the PHP code is fine. Besides, it is the very first line of PHP code, which creates the mysqli object, that is failing, so it is clearly something about the installation or configuration of the sqli extention that is preventing the librarry from loading or executing correctly, not a problem with the code itself.

Thank you all for your suggestions!

I finally got this issue resolved, though I don't fully understand it, and it may not work well for other people. The "fix" was the hostname used to log into SQL. I WAS using "pi@localhost". Changing that to "pi@127.0.0.1", got everything working, which one small issue that I just raised in a new thread.

Here is the tip that I got from someone on a PHP forum, that got it working:

My guess is this is due to mysql trying to use a unix socket to connect, but that socket does not exist. Mysql will try and use a unix socket instead of TCP when you specify 'localhost' as the hostname.

Quote

A Unix socket file is used if you do not specify a host name or if you specify the special host name localhost

Try changing the hostname in your connect call to 127.0.0.1 instead.

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