MongoDB with PHP
Hey Guys !
Its Arsalan Peerzada Here .. In this Blog we try to learn how to connect MongoDB with PHP using WAMP, because WAMP (Windows Apache MySQL PHP) is more dedicated to windows as compare to XAMPP or other Servers.
So lets Start with the Downloading ….
MongoDB : https://www.mongodb.org/downloads
Windows 32-bit or Windows 64-bit 2008 R2+ , In .Zip format.
Windows 32-bit or Windows 64-bit.
This picture will appear , but don’t worry it is not that much dangerous as is look like …
Open WAMPServer Installation Wizard. With This icon
Click on Next >
Select “I accept the agreement” and then click Next.
Select the default path and Click Next >
Select both and Click Next.
Choose Browser of your own choice or Just click Open
If you are not sure , leave the default values ..
Now Click Select Launch and then Click Finish …
You’ll see then WAMP icon in lower-right corner
Red Icon = All Services Stop .
Orange Icon = Partial Services are running .
Green Icon = All Services are running.
Open it ,
If Icon is Red , Orange or Green . Do not Worry we’ll configure it later.
Now Click Localhost , WAMPServer Page will Open .
If not ,
Open httpd and httpd.conf respectively and search Listen then change Every :80 to :8080
Open httpd-vhosts then change every :80 to :8080
Now your LocalHost will be LocalHost:8080
Now go to
Create a new folder , Copy the name of MongoDB zip file and rename the folder with it.
Extract the MongoDB zip file into this folder.
Now Create Folder , names conf , data , logs
In conf folder create a note pad file and save it in mongodb.conf and write this content in it.
# data directory
# log directory
# run on localhost for development
bind_ip = 127.0.0.1
# using port
port = 27017
Note :- adjust your mongodb version in the file.
Open the windows command prompt as administrator privilege and locate it to
Note :- adjust your mongodb version
Install MONGODB :
Execute this command to install Mongo DB as a windows service.
Command to execute:
mongod.exe –install –config C:\wamp\bin\mongodb-win32-x86_64-2008plus-3.0.1\conf\mongodb.conf –logpath C:\wamp\bin\mongodb-win32-x86_64-2008plus-3.0.1\logs\mongodb.log
Note :- adjust your mongodb versions
Set Environment Path:
Now set the Windows Environment Path
Right click on My Computer.
Click Advanced system settings.
Click Environment Variables button.
Click PATH on Variable column, then append this path.
Note :- adjust your mongodb and PHP versions
Now Open Command Prompt with Administrator and type mongod , you’ll see this
Check your MongoDB , Now Open another Command Prompt with administrator and type mongo
And some other commands to check your MongoDB database is working or not.
NOW ADD THE PHP Extension:
first of all you have to download the latest mongo dll files from
unzip the downloaded file and you will see a bunch of files like this:
now it is time to decide which dll file you have to use.
open your phpinfo() and search for word compiler. you will see sth like this:
so now according to my compiler version (MSVC11) I have to choose file with vc11 word inside their name. according to my PHP version which is 5.5.12 I have to choose file with word 5.5 inside their name.
now search for word thread safety in your phpinfo
and if it was enabled, it means that you have to choose file that doesn’t have word nts inside their name but if your thread safety was disabled then you need to choose file with word nts inside their name.
now according to your installation architecture you may choose files with x86_64 inside their name.
after choosing the appropriate dll file, rename it to
and copy paste it to your php extensions directory which is mostly located at
now open your php.ini file and add this at the end of it.
Edit your php.ini file to add the new extension.
wampmanager -> PHP -> php.ini
find extensions then add this line below other extension lines,
To use mongo with scripts run from the command line (PHP CLI) edit
and add the extension line again in there.
Copy all dll files from
Restart your system and Open WAMP
Run localhost:8080 by :-
and then click on the phpinfo() link on the homepage.
You should now see a section entitled ‘mongo’ with some configuration options listed. If you do the PHP MONGO extension is active.
Starting WAMP with MONGODB:
Now Open Notepad++ in c:\wamp\www
write this code
$m = newMongoClient();
echo “MongoDB Successfully Running”;
Now open your browser type LocalHost:8080\MyPhP.php
If this code work then your MongoDB+WAMP is running.
Today we learn how to Connect MongoDB with WAMP.
Now we are able to work with famous NoSQL MongoDB with PHP