MongoDB For Novices: Setting up MongoDB For PHP (Element three/3)

MongoDB For Novices: Setting up MongoDB For PHP (Element three/3)

kianleong . Posted in Miscellaneous 84

Ahead of diving into this, I suggest that you study the following connected articles if you have not:

  • Introduction to MongoDB
  • Standard MongoDB shell commands

The MongoDB server is built to currently perform with your current net server. The problem is that you’ll require to install drivers for your preferred backend language – PHP, Ruby, Node.js, Perl, what ever. I won’t go into the process of installing WAMP/MAMP since this is a bit off subject from Mongo.

But there are really simple-to-comply with tutorials which already exist for installing WAMP and installing MAMP on either Operating System.

Note: You can nonetheless work with your MongoDB server without a web server. But most applications would need this and that’s why I’m focusing mostly on MongoDB for internet development.

You can get some output from the MongoDB process by going to the localhost address using your installation’s specific port quantity.

MongoDB will default to 27017. This is the driver port and to view analytics/diagnostics we want to use 28017. So you may possibly access the MongoDB server information on your browser by going to:


This address ought to nonetheless work properly regardless of your local net server being online or not.

Right after you have WAMP or MAMP installed and running you can pay a visit to the localhost internet server on port :80 to see the default web page template.

Now I’m going to stroll you by way of installing the PHP driver, and we’ll finish up developing more than MongoDB’s PHP class library.

Setup the MongoDB PHP Drivers

Mac and Linux customers need to be in a position to install these drivers proper from the command line. Looking on the MongoDB PHP language docs we should install making use of pecl from the Pear Library of PHP code.

Right here’s the line of code you must run from terminal:

sudo apt-get install php5-dev php5-cli php-pear
sudo pecl set up mongo

If you already have Pear installed then you don’t need to have to run the initial line. That is only for PHP installs which are not updated to the most recent Pear library. But after the commands finish find your php.ini file and add the following bit of code:

You need to notice a comparable block of code somewhere midway down the file which has a slew of other lines mirroring extension=name. Most extensions are commented out but the lines without having a hash symbol(#) are at present active extensions.

Following you’ve added this line save &amp close the file, then restart your Apache internet server for the new changes to take effect.

Mongo PHP Extension on Windows

All customers on Windows will also need to edit their php.ini file. This can be accomplished straight from the WAMP context menu by clicking on the icon, then moving to PHP -> php.ini. You’ll want to add the identical line of code except the filename should be php_mongo.dll.

Also instead of installing by way of the command line it’s significantly simpler to download a copy of the extension and move this over manually.

Windows users should head over to this Github directory full of MongoDB PHP drivers. Find the newest release which supports your version of PHP (five.two, 5.3, 5.four) and download the .zip. When you extract the folder locate the extension which matches your version of PHP. In my case I’ll use php_mongo-1.two.12-5.3-vc9.dll and rename this to php_mongo.dll.

Now location this file straight inside your PHP extensions directory located in C:wampbinphpphp5.xext. If you have this file moved more than and the extension line of code added to your php.ini file then every thing ought to be good to go! Restart your internet server and open up a phpinfo() web page to view the benefits.

You can do a CTRL + F search for “mongo” and need to find information about the module itself.

Mongo Web Development with PHP

There is so considerably to talk about when it comes to net improvement and databases. This is only an introduction tutorial so we won’t be able to touch on numerous topics including customers, authentication, updating objects, a number of databases, etc. But let’s finish up by going over the PHP MongoDB class and how we can quickly connect into a database.

I’ll use our test DB in this example accessing our previously developed “shows” collection. We can pull all this information out employing PHP and show the contents on a webpage. I’m generating a new PHP file in my regional server root named shows.php with the following code:

// Config
$  dbhost = 'localhost'
$  dbname = 'test'

// Connect to test database
$  m = new Mongo("mongodb://$  dbhost")
$  db = $  m->$  dbname

// pick the collection
$  collection = $  db->shows

// pull a cursor query
$  cursor = $  collection->find()


What I’m undertaking is selecting our test database and further accessing the internal shows collection. We can run the uncover() function on any Mongo collection object to pull out a cursor with all the associated internal information.

Now to output this data onto the page let’s use var_dump() which is a significantly greater option than print_r(). Add this last block of code straight underneath the $ cursor variable.

foreach($  cursor as $  document) 
 var_dump($  document)

This foreach() loop will go via the cursor benefits and output variable information for each and every internal array. We need to have 3 objects displaying the data added into our Television Shows earlier. You will notice there is also another crucial named _id which is the automatic object ID produced for every single document.

I have to recommend just going via Google or the MongoDB docs to understand far more about the PHP class. There is so a lot details that it can’t all be crammed into this introductory tutorial. But this small PHP script ought to be an example of just how versatile Mongo databases truly are! No confusing SQL commands, no requirements for authentication(unless needed), and all the syntax is extremely effortless to read.

Final Thoughts

Developers who are familiar with databases may nevertheless be pushing by means of this report with difficulty. Going through this tutorial two or three instances may possibly nevertheless even leave you confused on some terminology. But don’t get discouraged by Mongo’s initial hurdles. Even a week’s worth of practice is adequate to nail down a really excellent understanding.

The Mongo open source database technique is schemaless and swiftly scalable in comparison to other rival systems. You are not restricted to columns or tables and inserting data can be speedily accomplished by means of JSON-like syntax. Also, connecting your net applications using PHP is often less complicated than MySQL/MSSQL as soon as you understand the code.

I do hope this newbie’s tutorial can give a solid overview from MongoDB terminology to installation, shell commands, and light web improvement.

Overall Mongo may not be your 1st database selection when developing a new internet project. But the program is safe, extremely reputable, and slowly gaining interest with a expanding community of dedicated supporters.