Error 'Class' MongoClient' not found "[closed]

3

I recently installed MongoDB on my Macbook, I installed the extension in native PHP, which I'm using along with native apache. The problem is this, if I run the Web page in 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 run with the command php public/index.php I get 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);
    
asked by anonymous 03.01.2016 / 05:14

2 answers

1

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

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

I do not know how to install the installation files on MAC OS, but generally in Linux environments you can have an extension that is enabled for cli but not for cgi and httpd (Apache, for example). For example, see:

Notice that under each SAPI (apache2, cli, cgi) there is a corresponding php.ini and a directory called conf.d content .ini files that are usually of a specific extension configuration (ex .: 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 sure that mongo.ini (according to your comment there is a file of that 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 in MAC OS). Maybe even copying the content from mongo.ini to php.ini main resolves.

Anyway, keep in mind that this extension is obsolete.

    
03.01.2016 / 22:46
3

The php.net documentation has the following warning:

  

Attention: The extension that defines this class is deprecated. Instead you should use the MongoDB extension. Alternatives to this class:

     

In other words, you have probably installed the MongoDB extension for the 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 in php.ini this:

extension=mongodb.so

If it's Windows:

extension=php_mongodb.dll

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

$ sudo pecl install mongodb
    
03.01.2016 / 22:00