5

I just can't seem to find any clear instructions on how exactly you do that for PHP 7 running on a machine with Windows 7 installed. I tried a couple of online tutorials, but nothing seems to have worked out for me so far. It's possible that some of you may have had an experience similar to mine and it would be absolutely wonderful if you could share that experience with me: specifically, what exactly you did in order to to get this issue resolved.

Here's what I did:

I downloaded the latest dll library for PHP 7 (mongodb-1.1.2.tgz) from here: PECL :: Package :: mongodb :: 1.1.2, placed the file php_mongodb.dll from the archive into the ext directory where I keep my PHP installation, added the line extension=php_mongodb.dll to the php.ini file (after all these steps, Apache was restarted, of course). The section for mongodb does show up as a result of running the phpinfo() function:

enter image description here

And now I'm trying to run this simple script:

<?php

    $connection = new MongoClient();

?>

And what I get back is the following error (I have broken the lines a little bit for better readability):

Fatal error: Uncaught Error: Class 'MongoClient' not found in
C:\Apache24\htdocs\test2.php:3 Stack trace: #0 {main} thrown in
C:\Apache24\htdocs\test2.php on line 3
  • Possible duplicate of [Connecting to mongodb using PDO driver](http://stackoverflow.com/questions/8255440/connecting-to-mongodb-using-pdo-driver), the question is not quite the same, but the answer is. – Mike 'Pomax' Kamermans Feb 03 '16 at 01:32
  • alternatively, I assume you googled and found https://docs.mongodb.org/ecosystem/drivers/php/ but if not: that's the most official of possible source on how to use mongo with php – Mike 'Pomax' Kamermans Feb 03 '16 at 01:34
  • note that if php_info() does not report the mongo extension, it was not installed correctly. Did you "install" manually or did you use PECL? If manually, can you please explain what you did, and can you show the extension section of your `php.ini`? – Mike 'Pomax' Kamermans Feb 03 '16 at 01:49
  • I did not use PECL. I installed everything manually as I recounted in my post. –  Feb 03 '16 at 02:13
  • if it shows up correctly then the driver's installed just fine, where did you get the `MongoClient` syntax? According to all the docs I'm seeing linked to by mongodb, the syntax is `$client = new MongoDB\Driver\Client("mongodb://localhost:27017");` – Mike 'Pomax' Kamermans Feb 03 '16 at 16:13
  • How about the official PHP documentation: http://php.net/manual/en/class.mongoclient.php –  Feb 03 '16 at 20:34
  • okay, but let's pay attention to that *"**Warning** This extension that defines this class is deprecated. the MongoDB extension should be used"* part, which ... is pretty important? It even tells us what to use instead ([MongoDB\Driver\Manager](http://php.net/manual/en/class.mongodb-driver-manager.php)), so let's follow that advice. – Mike 'Pomax' Kamermans Feb 03 '16 at 20:38
  • I went ahead and checked that real quick and it looks like that it's working now (at least the PHP engine doesn't throw an error). I'll give it a more thorough check later though. In the mean time, feel free to post that as an answer and I'll mark it as such a bit later when I get my busy hands back on MongoDB. –  Feb 03 '16 at 20:55

1 Answers1

3

MongoDB offers its own driver, with installation instructions on how to set it up, after which you'll have to use the the MongoDB Driver API, rather than the now obsolete MongoClient way of connecting. If phpinfo() shows the mongodb extension working, then you should be able to connect to it using the updated syntax:

$client = new MongoDB\Driver\Manager("mongodb://localhost:....");
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153