Need help with Mongo and PHP 7.4

Hello,

I have a PHP v7.4 server with mongo enabled.

I just tried the suggested connection and I got a failure.

$client = new MongoDB\Client(
    'mongodb+srv://USER:PASSWORD@CLUSTER/test?retryWrites=true&w=majority'
);
$db = $client->test;

Could do with some help working out what the issue is.

I have seen several different versions of mongo usage in php which makes it a little confusing.

Also, I couldn’t find a clear link from php 7.4 to mongo driver version. Because I saw a list of other version numbers of the mongo php version. E.g https://docs.mongodb.com/drivers/php under compatibility.

Thanks in advance

Possibly you could get the help you desire if you posted the error you are encountering?

PS I’d drop the +srv from the URI.

Use the current version. Follow the installation instructions. I’m on PHP 7.4 with the driver + library. Your example should work.

I was coming here to post the error, remembered this morning that I hadn’t shared that important bit of information.

Fatal error : Uncaught Error: Class ‘MongoDB\Client’ not found

Thanks Jack,

This is the version I most recently tried, it’s the one which gives the error I just shared.

Just to clarify, the error refers to line 21 in my script.

Line 21 is
$client = new MongoDB\Client(

Okay, are you sure you followed the instructions on installation, especially

Finally, add the following line to your php.ini file:

extension=mongodb.so

And does your PHP file start with
require_once '/whatever/path/vendor/autoload.php';

Thanks Josh,

What does the autoload.php containe?

is vendor meant to be mongo or something? in the requireonce? or just vendor. What sort if area is the whatever/path location meant to be?

I will check the php.ini file.

Many thanks
Russell

autoload.php is a conventional name for a file which loads a bunch of other php files.

PHP packages typically come with an autoload.php file.

Composer, the tool that installs the MongoDB PHP library on top of PHP’s own MongoDB driver, installs all the packages you install via Composer in a directory called vendor. It’s just like npm in Javascript where everything is installed in a project subdirectory called node_modules.

So typically for PHP + MongoDB you:

  • make sure the PHP driver mongodb.so is installed and called out in your php.ini files for web and for cli
  • create your project directory
  • use Composer to install MongoDB’s PHP Library in your project directory, which results in you having a ./vendor subdirectory of your project

Then, in your program code, require_once ./vendor/autoload.php which loads all the modules Composer installed in your ./vendor subdirectory of your project.

2 Likes

Thanks Jack, much appreciated.

1 Like

Holler if you get stuck! :slight_smile:

7 posts were merged into an existing topic: Class ‘MongoDB\Client’ not found while following offical installation docs

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