Installing PHP Driver

25 views
Skip to first unread message

rober...@yahoo.com

unread,
Jan 24, 2020, 3:48:31 PM1/24/20
to mongodb-user
I've read numerous on-line posts for installing MongoDB drivers for PHP and nothing seems to be working for me. Can someone give me a simple step by step procedure to follow to try and get Mongo working in my PHP code (it's on a Windows 10 laptop, Mongo version 4.2.2)? I want to use MongoDB on my laptop and experiment with writing PHP to communicate back and forth with Mongo (not looking to use the Cloud at this point in time). Any help would be GREATLY appreciated. Cheers.

Jeremy Mikola

unread,
Jan 24, 2020, 4:11:13 PM1/24/20
to mongod...@googlegroups.com
Note that the PHP driver consists of two parts: a low-level "mongodb" extension, which is available on PECL, and a higher-level library, which is available via Composer. The library depends on the extension. Although you can use the extension alone, we recommend installing both and developing with the library, as it provides a much nicer API.

Installing the "mongodb" extension on Windows entails downloading the appropriate DLL file from PECL and modifying your environment's php.ini file to load that extension (as described in https://www.php.net/manual/en/mongodb.installation.windows.php). If you navigate to https://pecl.php.net/package/mongodb, you'll find a linked page with DLLs for each release. For example, the 1.6.1 DLLs are available from https://pecl.php.net/package/mongodb/1.6.1/windows.

The most important thing will be to figure out which DLL to download, based on your system's phpinfo() output. That is described in the common PHP.net docs for installing Windows extensions, so I encourage you to read https://www.php.net/manual/en/install.pecl.windows.php#install.pecl.windows.which thoroughly. Alternatively, you can make this a game of trial and error. If the extension is loaded successfully, you should be able to see it in phpinfo(). Additionally, phpversion('mongodb') should return a version number of the installed extension.

Once that is sorted, you can install the library by requiring the "mongodb/mongodb" package with Composer, as described on https://docs.mongodb.com/php-library/current/tutorial/install-php-library/. That page also notes that you may need to manually configure autoloading if you're not using Composer; however, we don't dive into that process in very much detail because it's not a recommended approach.

One last thing that bears mentioning is to ensure the extension is available in both your web and CLI environments. Those often have separate php.ini files, so you'd need to load the extension in both. Since Composer generally runs via a CLI environment, folks sometimes encounter an error that "mongodb/mongodb" cannot be installed because the "mongodb" extension is not available, despite having configured it properly for their web environment.

On Fri, Jan 24, 2020 at 3:48 PM 'rober...@yahoo.com' via mongodb-user <mongod...@googlegroups.com> wrote:
I've read numerous on-line posts for installing MongoDB drivers for PHP and nothing seems to be working for me. Can someone give me a simple step by step procedure to follow to try and get Mongo working in my PHP code (it's on a Windows 10 laptop, Mongo version 4.2.2)? I want to use MongoDB on my laptop and experiment with writing PHP to communicate back and forth with Mongo (not looking to use the Cloud at this point in time). Any help would be GREATLY appreciated. Cheers.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/74125147-5cc7-4eb0-8fc9-934691ae6b69%40googlegroups.com.

rober...@yahoo.com

unread,
Jan 24, 2020, 6:18:44 PM1/24/20
to mongodb-user
Thanks Jeremy. I grabbed the dll, restarted IIS after trying a few of them but phpinfo showed nothing in the browser regarding Mongo. Oh and as changed the php.ini before restarting IIS too. Still got no mention of Mongo in the php info command in the browser.
I tried running the command “composer require MongoDB/MongoDB” in a few ways too but one of the error messages was “Unable to load dynamic library ‘C:\php\ext\php_mongodb.dll’”. I don’t have php in that directory but I couldn’t find where to change this. PHP is under a different directory on my laptop altogether.

Jeremy Mikola

unread,
Jan 24, 2020, 6:42:35 PM1/24/20
to mongod...@googlegroups.com
On Fri, Jan 24, 2020 at 6:18 PM 'rober...@yahoo.com' via mongodb-user <mongod...@googlegroups.com> wrote:
Thanks Jeremy. I grabbed the dll, restarted IIS after trying a few of them but phpinfo showed nothing in the browser regarding Mongo. Oh and as changed the php.ini before restarting IIS too. Still got no mention of Mongo in the php info command in the browser.
I tried running the command “composer require MongoDB/MongoDB” in a few ways too but one of the error messages was “Unable to load dynamic library ‘C:\php\ext\php_mongodb.dll’”. I don’t have php in that directory but I couldn’t find where to change this. PHP is under a different directory on my laptop altogether.

https://stackoverflow.com/a/5282287/162228 suggests that "Unable to load dynamic library" may be reported if PHP cannot find the DLL file. If you've only specified `extension=php_mongodb.dll` in php.ini, then PHP would expect to find it in the directory indicated by the extension_dir setting.

Resolving Problems in the common PHP.net page I linked above also addresses this:

Common problems are the location of the DLL, the value of the "extension_dir" setting inside php.ini and compile-time setting mismatches

phpinfo() should be able to tell you the directory where PHP would be looking for extensions. Alternatively, you can specify an absolute path to the DLL file.

If you only saw an "Unable to load dynamic library" error from the CLI environment, I'd suggest you check the error reporting level for your web environment. If you don't have the web SAPI configured to display all errors in the HTTP response, then they should at least be directed to a log file that you can review for debugging purposes.
 

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.

rober...@yahoo.com

unread,
Jan 24, 2020, 8:57:38 PM1/24/20
to mongodb-user
I must have put my eggs in the one basket. I had modified what I thought was the php.ini but it looks like I grabbed an old php.ini.ini and changed it. I should have double checked the results from the phpinfo like you suggested in the first place. Okay, so that part looks resolved. phpversion('mongodb') in my test PHP script now shows 1.5.3 in the browser.

When running "composer require MongoDB/MongoDB" which directory should I be in? My apologies. It's been years since I've done much in the way of installing anything. I've been mainly writing CGI applications using Perl with either MySQL databases or xml documents.
To unsubscribe from this group and stop receiving emails from it, send an email to mongod...@googlegroups.com.

Jeremy Mikola

unread,
Jan 26, 2020, 10:21:47 PM1/26/20
to mongod...@googlegroups.com
On Fri, Jan 24, 2020 at 8:57 PM 'rober...@yahoo.com' via mongodb-user <mongod...@googlegroups.com> wrote:
I must have put my eggs in the one basket. I had modified what I thought was the php.ini but it looks like I grabbed an old php.ini.ini and changed it. I should have double checked the results from the phpinfo like you suggested in the first place. Okay, so that part looks resolved. phpversion('mongodb') in my test PHP script now shows 1.5.3 in the browser.

When running "composer require MongoDB/MongoDB" which directory should I be in? My apologies. It's been years since I've done much in the way of installing anything. I've been mainly writing CGI applications using Perl with either MySQL databases or xml documents.

Composer commands would typically be run from the root directory of your project. When composer runs, it will look for a composer.json file in the current directory and organize all dependencies under a vendor/ sub-directory. If you've not worked with Composer before, I'd suggest reading through https://getcomposer.org/doc/01-basic-usage.md for an introduction. Alternatively, the SymfonyCasts video introduction to Composer may be helpful.

Lastly, the package is "mongodb/mongodb". I'm not sure if Composer cares about case, but I'd suggest sticking to the lowercase, canonical name to avoid any potential issues.
 


On Friday, January 24, 2020 at 6:42:35 PM UTC-5, Jeremy Mikola wrote:


On Fri, Jan 24, 2020 at 6:18 PM 'rober...@yahoo.com' via mongodb-user <mongod...@googlegroups.com> wrote:
Thanks Jeremy. I grabbed the dll, restarted IIS after trying a few of them but phpinfo showed nothing in the browser regarding Mongo. Oh and as changed the php.ini before restarting IIS too. Still got no mention of Mongo in the php info command in the browser.
I tried running the command “composer require MongoDB/MongoDB” in a few ways too but one of the error messages was “Unable to load dynamic library ‘C:\php\ext\php_mongodb.dll’”. I don’t have php in that directory but I couldn’t find where to change this. PHP is under a different directory on my laptop altogether.

https://stackoverflow.com/a/5282287/162228 suggests that "Unable to load dynamic library" may be reported if PHP cannot find the DLL file. If you've only specified `extension=php_mongodb.dll` in php.ini, then PHP would expect to find it in the directory indicated by the extension_dir setting.

Resolving Problems in the common PHP.net page I linked above also addresses this:

Common problems are the location of the DLL, the value of the "extension_dir" setting inside php.ini and compile-time setting mismatches

phpinfo() should be able to tell you the directory where PHP would be looking for extensions. Alternatively, you can specify an absolute path to the DLL file.

If you only saw an "Unable to load dynamic library" error from the CLI environment, I'd suggest you check the error reporting level for your web environment. If you don't have the web SAPI configured to display all errors in the HTTP response, then they should at least be directed to a log file that you can review for debugging purposes.
 

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/f8434a6e-65a5-4922-9a1b-653dfcc884f7%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/4e970750-7076-42f4-87ca-7c8a67d7fec6%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages