Ruben
Ruben

Reputation: 23

MongoClient not working with php

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.

Upvotes: 2

Views: 4969

Answers (1)

bartvanraaij
bartvanraaij

Reputation: 343

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

Upvotes: 2

Related Questions