1

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
LWC
  • 1,084
  • 1
  • 10
  • 28
  • 4
    You mean like, apart from PDO? – Félix Adriyel Gagnon-Grenier Jun 13 '23 at 20:31
  • 1
    Have you read what PHP offers you? [Overview of the MySQL PHP drivers - Choosing an API](https://www.php.net/manual/en/mysqlinfo.api.choosing.php) – maio290 Jun 13 '23 at 20:32
  • This link compares between mysqli and PDO. I believe it's likely PDO will trigger a similar error unless you enable `nd_pdo_mysql`, which again means enabling a non recommended option, @Félix Adriyel Gagnon-Grenier and @maio290. – LWC Jun 13 '23 at 20:39
  • I use PDO with php 8.2 currently. The article you share is explicitly about mysqli. – Félix Adriyel Gagnon-Grenier Jun 13 '23 at 20:44
  • True. I've just tried it and `new PDO` works even without `nd_pdo_mysql`. So are you saying one should choose PDO over mysqli simply over the lack of need to enable a non default extension? This advantage isn't even mentioned in the comparison. In fact, the only benefit there to PDO is client-side Prepared Statements, while listing several disadvantages. – LWC Jun 13 '23 at 20:51
  • 2
    I'm just used to PDO, is all. I er... really do not think that the problem you observe is linked to your article however. What the article says, is that one can no longer compile php with libmysql, but if your host did compile it *they assuredly did so using mysqlnd*, if libmysql is no longer available. I do not believe that php 8.2 doesn't ship with mysqli anymore, unless someone messed up things greatly. Are you certain you in fact enabled the mysqli extension? – Félix Adriyel Gagnon-Grenier Jun 13 '23 at 21:00
  • `nd_mysqli` is disabled by default. Once I enable it, the errors of lack of mysqli immediately go away and the db connection works. Once I disable `nd_mysqli` again, the errors immediately return. – LWC Jun 13 '23 at 21:16
  • 2
    Right. I think I start to see clearly. I'm still confused by 1. why is there no mysqli extension box in your cpanel. I can see one in mine (along the nd_mysqli one). 2. Who's doing that non-recommendation anyway? 3. how come you would be presented with no way to continue using mysqli. I would start looking for a new host before rewriting away from mysqli, a host must offer a way for people to use mysqli with php 8.2. – Félix Adriyel Gagnon-Grenier Jun 13 '23 at 21:45
  • Maybe tomorrow let's move this to a chat if you like. 1. The "as explained here" link in the OP suggests it's a known scenario. 2. I do it because that's the way to continue using mysqli. 3. The host does let me enable `nd_mysqli` which fixes everything - they said they can't recommend it but stated it's my choice. – LWC Jun 13 '23 at 22:51
  • sure, you can ping me in the [php chat room](https://chat.stackoverflow.com/rooms/11/php) if you want. if I don't answer there's a lot of very knowledgeable people around anyway (that may or may not be interested in this question, so navigate accordingly) – Félix Adriyel Gagnon-Grenier Jun 14 '23 at 00:41
  • 2
    This has nothing to do with how anything was built / compiled. If the classes / functions aren't available, then the associated extension has not been enabled. Simple as that – Phil Jun 14 '23 at 01:12
  • But given that mysqli is quite a standard, i just wonder why would it be turned off by default. – LWC Jun 14 '23 at 16:17
  • @LWC That is simply how it works. Php comes with many extensions compiled in, but none are enabled. Developers can then choose which ones they need. Why would mysqli be enabled over pdo, for instance? The only real problem here, is why does your host not provide a valid mysqli (not nd_mysqli) extension with the php executable. that's super weird. I'd push back really hard on that. it's not acceptable. A php installation has a mysqli extension that can be enabled, otherwise it's not php, it's some dastardly corruption. – Félix Adriyel Gagnon-Grenier Jun 14 '23 at 16:47
  • But that's exactly it - PDO works out of the box. Regarding the host, what does enabling `nd_mysqli` not count as providing a valid mysqli extension? – LWC Jun 14 '23 at 16:49
  • 1
    ask them. they're the one compiling the software they make available on your server. all we're saying is that if mysqli functions are not available, it's because the extension is not enabled. if your host does not provide a mysqli extension you can use, that's extremely concerning. – Félix Adriyel Gagnon-Grenier Jun 14 '23 at 16:50
  • do come hang out in the php chat @LWC, I feel we've reached, and possibly went over, the generally accepted limit of comments ;-) – Félix Adriyel Gagnon-Grenier Jun 14 '23 at 16:54
  • I did ask them. Judging from their answers it just comes this way. They just suggest to enable `nd_mysqli` which fixes everything despite the risk of enabling non default extensions. – LWC Jun 14 '23 at 16:54
  • 1
    anyway. last thing I'll say here @LWC is: a host must offer a way they recommend of using mysqli. this is not arguable. Either they offer a php installation that works, that means has a mysqli (or nd_mysqli) extension they support, or they do not in fact offer php, they offer a custom thing that looks like php. by the way, I cannot find anything to the effect that nd_mysqli is not recommended, it looks like it's simply the name given to mysqli by cpanel hosters. I really doubt the person who told you "that's not recommended" has any clue what they're saying. – Félix Adriyel Gagnon-Grenier Jun 14 '23 at 17:08
  • That person just said they can't recommend anything that isn't a default. – LWC Jun 14 '23 at 17:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/254078/discussion-between-felix-adriyel-gagnon-grenier-and-lwc). – Félix Adriyel Gagnon-Grenier Jun 14 '23 at 18:37

1 Answers1

0

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Can you source that PDO is recommended over mysqli? Also, how come PDO works even without nd_pdo_mysql? – LWC Jun 16 '23 at 23:06
  • @lwc PDO can still be compiled against the libmysql library. So cPanel still offers both variants. – Dharman Jun 17 '23 at 06:11