2

I'm trying to use mongodb with php but I keep getting this error

("Class 'MongoClient' not found")

whenever I try this code:

$m = new MongoClient();

The weird part is that if I run a get_loaded_extensions(), mongodb shows up and it also shows up on the phpinfo(). I also have extension=mongodb.so on the php.ini file.

I'm on debian running MongoDB shell version: 2.6.11 and php is running mongodb version 1.1.2.

2
  • What error do you get? Commented Feb 25, 2016 at 11:50
  • "Class 'MongoClient' not found"
    – Ruben
    Commented Feb 25, 2016 at 11:51

1 Answer 1

1

You're mixing up the Mongoand MongoDB extensions. The (deprecated) Mongo extension loads via mongo.so and provides MongoClient. You're using the newer and preferred MongoDB extension (mongodb.so) so you should be using:

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

See: http://php.net/manual/en/set.mongodb.php vs https://www.php.net/manual/en/mongo.setup.php

4
  • 1
    Now I get "Class 'MongoDB\Client' not found" =/
    – Ruben
    Commented Feb 25, 2016 at 12:03
  • 1
    I found out in the documentation you put! It should be $client = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
    – Ruben
    Commented Feb 25, 2016 at 12:10
  • Ah, the client library isn't automatically included. You should install that too: php.net/manual/en/mongodb.tutorial.library.php Commented Feb 25, 2016 at 12:11
  • Ya, that worked too. Thansk a lot! .. Almost all the problem came from the fact that when doing a google search for "php mongodb", you actually get the deprecated extension (Mongo)
    – Ruben
    Commented Feb 25, 2016 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.