How to connect in PHP to a MySQL database without getting errors due to cPanel?

1.9k Views Asked by At

This basic commands:

new mysqli($servername, $username, $password);
mysqli_connect($servername, $username, $password);

are blocked in PHP 8.2+ with Uncaught Error: Class "mysqli" not found or Uncaught Error: Call to undefined function mysqli_connect() (depends on which one you used) seemingly due to PHP 8.2: MySQLi can no longer be compiled with libmysqli (which suggests to use the mysqlnd library instead of the libmysql library).

The way to bypass it in cPanel, as explained here (and in a past version here), is to check the usually turned off nd_mysqli, since obviously having mysqlnd (which is checked by default) is not enough.

But since my hosting explained "the reason why the "nd_mysqli" was disabled is because the module is not recommended", is there another PHP command that is recommended for PHP 8.2+ to connect to a MySQL database from PHP that will not trigger any errors or require any special setup?

MySQL in cPanel

1

There are 1 best solutions below

2
Dharman On

The recommended extension has always been PDO. However, if your site is already written using mysqli, you can continue using it even on PHP 8.2.

CPanel is stupid and their idiotic configuration has confused a lot of people. They compiled both PDO and mysqli with libmysql by default and only offered the better mysqlnd variant as a non-standard extension with prefix nd_. Users should never use the libmysql variants and on PHP 8.2 it's no longer possible to even offer mysqli with that library. The correct extensions that should always be enabled are:

  • nd_pdo_mysql
  • nd_mysqli

I don't know what they offer under the name mysqlnd as it's not an extension that can be enabled or disabled. Offering this as an option in configuration is ridiculous.

I can't understand a reason why cPanel would not recommend the nd_* variants.

See also https://stackoverflow.com/a/73902084/1839439