29

I'm trying to make this code run:

<?php

$m = new MongoClient("mongodb://54.72.237.242"); 
$db = $m->tilbud; 

?>

Everytime I got the same error:

Fatal error: Class 'MongoClient' not found in C:\xampp\htdocs\conexion.php

I've been reading about this problem the whole day but anything solves my issue (I guess it's something I'm doing wrong).

I downloaded the php_mongo.dll, I copied it in xampp/php/ext and I added extension=php_mongo.dll in the php.ini archive.

I've added 4 more dll's because I'm not sure which one I have to use:

  • extension=php_mongo-1.5.4-5.5-vc11-nts
  • extension=php_mongo-1.5.4-5.5-vc11
  • extension=php_mongo-1.5.4-5.5-vc11-nts-x86_64
  • extension=php_mongo-1.5.4-5.5-vc11-x86_64

So now im getting 5 warnings instead of one. At the end I guess one of them will work and I'll delete the other 4.

Things I tried and I'm sure they are ok:

  • The extension_dir is pointing to the correct folder.
  • The php.ini that I modified is the one that xammp loads.
  • Phpinfo dosen't show anything about mongo.

What more can I try ?

Edit

I tried

echo extension_loaded("mongo") ? "loaded\n" : "not loaded\n";

and it always says 'not loaded'.

Edit

Finally! The problem was the dll's name. It has to be 'php_mongo.dll' and I was trying to load the full name dll as I said at the begining of this post. So I changed the correct dll for me (extension=php_mongo-1.5.4-5.5-vc11) for extension=php_mongo.dll and voilà!

aynber
  • 22,380
  • 8
  • 50
  • 63

7 Answers7

38

You have not installed MongoDB PHP driver please see this link http://www.php.net/manual/en/mongo.installation.php

Update sources

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update

Install MongoDB PHP Driver

sudo apt-get install php5-dev php5-cli php-pear -y
sudo pecl install mongo

Open your php.ini file and add to it:

extension=mongo.so

Restart apache

sudo /etc/init.d/apache2 restart

Other helping info:

this should help to find your php.ini file:

php -i | grep 'Configuration File'

On Ubuntu it shows this:

Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini

take a note, that you run this command from cli (command line) so for your true php.ini go to folder apache2 instead of cli :)

Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109
  • 3
    Use `printf "\n" | pecl install mongo` if You need to integrate it into an install script. – czerasz Nov 18 '14 at 13:43
  • 5
    In the original question there is a reference to `C:\xampp\htdocs` which indicates that they are using Windows, and therefore none of these commands will help them at all. – nevada_scout May 10 '15 at 16:44
  • 1
    good point nevada_scount, thanks. I hope my answer will still be usefull to all who are using linux. – Lukas Liesis May 10 '15 at 19:26
  • **Solved**! I have problem with extension name, extension= **mongodb.so** -> **mongo.so** – WOLFF Apr 05 '17 at 08:34
29

For those arriving on this page with PHP 7 installed:

The MongoCLient class was provided by pecl install mongo. But pecl/mongo is not available for php7 and deprecated in favor of pecl/mongodb. But with pecl/mongodb you'll need to use MongoDB\Driver\Manager instead of MongoClient (warning on page says so too).

See here for further reading.

This said, you will need an abstraction layer on top of the PHP MongoDB\Driver\Manager. This is provided by mongodb/mongo-php-library.

You will need to refactor stuff like:

  • \MongoClient to \MongoDB\Client
  • \MongoCollection to \MongoDB\Collection
  • \MongoClient->selectDB to \MongoDB\Client->selectDatabase
  • \MongoClient->listDBs to \MongoDB\Client->listDatabases
    • also output is not an array but an iterator, so you'll need to use iterator_to_array, along with edits to how you use the resulting object
  • \MongoCollection->getName to \MongoDB\Collection->getCollectionName
  • \MongoCollection->update to \MongoDB\Collection->updateOne or updateMany
  • \MongoCollection->remove to \MongoDB\Collection->deleteOne
  • \MongoCollection->batchInsert to \MongoDB\Collection->insertMany
Community
  • 1
  • 1
Shadi
  • 9,742
  • 4
  • 43
  • 65
  • 7
    why are they making us refactor everything? So sad – inorganik Jul 24 '16 at 21:29
  • 2
    You can also use this plugin on top of php mongodb client to use the old methods and classes on the new driver : https://github.com/alcaeus/mongo-php-adapter – Big Zak Aug 12 '16 at 01:26
4

The answer is indeed to follow the instructions. I was missing the very important require line that must come before creating the new mongodb object:

<?php

require 'vendor/autoload.php';

$client = new MongoDB\Client("mongodb://localhost:27017");

And of course you need to run this command in the root of your project as per the instructions:

composer require mongodb/mongodb
mcmacerson
  • 885
  • 2
  • 14
  • 17
3

Getting the same error and now it's solved.
I am using Linux Mint. To solve this issue I added extension=mongo.so in two directories:

  • /etc/php5/cli/php.ini
  • /etc/php5/apache2/php.ini
felipsmartins
  • 13,269
  • 4
  • 48
  • 56
Biswajit Panday
  • 817
  • 10
  • 20
3

install the driver, I have for example php5.6:

sudo apt-get install php5.6-mongo
FelixSFD
  • 6,052
  • 10
  • 43
  • 117
  • +1 to this response, but it helps me, becose i've added before this repository https://launchpad.net/~ondrej/+archive/ubuntu/php – montie Apr 01 '17 at 07:20
2

From this page:

Note: Additional DLL dependencies for Windows Users:

In order for this extension to work, there are DLL files that must be available to the Windows system PATH

M D P
  • 4,525
  • 2
  • 23
  • 35
0

your php version and dll file version should be same check it if versions are not same then update your xampp php according to the available dll version.