"Class 'Mongoclient' not found" error

Asked

Viewed 1,807 times

3

I recently installed in my Macbook Mongodb, I installed the extension in native PHP, which I am using together with native apache. The problem is this, if I run the web page on Apache I get the following message:

Fatal error: Class 'MongoClient' not found in /Applications/XAMPP/xamppfiles/docs/www.alice.local/Branch/academia/public/index.php on line 6

But if I execute with command php public/index.php I receive the message as follows:

 object(MongoClient)#1 (4) {
  ["connected"]=>
  bool(true)
  ["status"]=>
  NULL
  ["server":protected]=>
  NULL
  ["persistent":protected]=>
  NULL
}

PHP file:

 <?php
    ini_set('display_errors',1);
    ini_set('display_startup_erros',1);
    error_reporting(E_ALL);

    $c = new MongoClient();
    var_dump($c);
  • What do you mean by " did I install the extension in native PHP"? Also, you made some changes in some php.ini?

  • he automatically created a file called Mongo.ini. I did the native PHP installation because Mac Os already comes with PHP and Apache natively.

2 answers

3

In the documentation of php.net there is the following warning:

Heed: The extension that defines this class is obsolete. Instead you should use the extension Mongodb. Alternatives for this class:

Which means you probably installed the extension to Mongodb for new functions and classes.

The correct use would be this:

<?php
error_reporting(E_ALL|E_STRICT);

$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
var_dump($manager);

Installing the extension

Add this in php.ini:

extension=mongodb.so

If it is Windows:

extension=php_mongodb.dll

You need to download the extension for the same version of your php, in the case as it requires PHP5.4+ or HHVM 3.9+, being PHP you can try to install via pecl in the terminal:

$ sudo pecl install mongodb

1


Of the two:
Or you have two versions of the PHP interpreter and your apache is configured with one of them;

You have the configuration php.ini , mongo.ini available only for the version cli (console), which is more likely.

I don’t know how to dispose the installation files on MAC OS, but usually in Linux environments you may have an extension that is enabled for cli but not for cgi and httpd (Apache, for example). For example, see: inserir a descrição da imagem aqui

Note that under each SAPI (apache2, cli, cgi) there is a corresponding php.ini and a directory called conf.d contento files (or symbolic link) .ini which are usually set to a specific extension (e.g., Mongo.ini, Xdebug.ini, etc). So what I mean is:
If you have a configuration file that enables a certain extension only under one of these interfaces (apache2, cli, cgi) it will only be available when you access PHP through this interface.

In your case, I’m almost certain that the mongo.ini (according to your comment there is a file of this somewhere, but you did not say) is under something like php5/cli/conf.d/mongo.ini. If this is the case, just copy or create a symbolic link somewhere like php5/apache/conf.d (but again, I’m not sure how these files are arranged on MAC OS). Maybe even copying the contents of mongo.ini to the php.ini main resolve.

Anyway, keep in mind that this extension is obsolete.

  • That was it, the CLI had the ini files in one folder, and apache was in another. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.