Mongo DB with PHP using XAMPP in Windows

Mongo DB with PHP using XAMPP in Windows

I just wanted to try mongodb with php for an idea that I had with my friend. So I just started it with basic setup example. MongoDB is cool and very simple to use. If you are using Windows Machine to setup Mongo, here is the steps.

Here is the installation steps for Windows:

  1. Install XAMPP
  2. You should have PHP 5.3 and above. Check it through (localhost/xampp/phpinfo.php)
  3. Download mongodb from their official site
  4. Extract the archive. Open command prompt and goto the bin folder inside that.
  5. Type “mkdir c:\mongodb\db”
  6. Type “mongod.exe –dbpath=C:\mongodb\db”
  7. Download windows driver for mongodb from GitHub page. Download the mongo-1.1.4.zip build that works for me
  8. Extract the archive. And find the build suitable to your computer. Copy the php_mongo.dll and paste it inside C:\xampp\php\ext folder
  9. Open php.ini file from c:\xampp\php
  10. Add “extension=php_mongo.dll” line to the file
  11. Save it and close. Restart apache after that
  12. Try this example code from php.net tutorial

Thats it!

// select a collection (analogous to a relational database’s table)
$collection = $db->pics;
// add a record
$obj = array( “title” => “RajiniKanth”, “author” => “karthi” );
$collection->insert($obj);
// add another record, with a different “shape”
$obj = array( “title” => “Trisha”, “online” => true );
$collection->insert($obj);
// find everything in the collection
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $obj) {
echo $obj[“title”] . “\n”;
}

One thought on “Mongo DB with PHP using XAMPP in Windows

  1. Thank you. I was looking for this only. I had issues with the latest windows driver for mongo db.

Leave a Reply

Your email address will not be published.