MongoDB For Beginners: Setting up MongoDB For PHP (Part 3/3)
Before diving into this, I recommend that you read the following related articles if you have not:
The MongoDB server is built to already work with your current web server. The problem is that you’ll need to install drivers for your preferred backend language – PHP, Ruby, Node.js, Perl, whatever. I won’t go into the process of installing WAMP/MAMP because this is a bit off topic from Mongo.
But there are very easy-to-follow tutorials which already exist for installing WAMP and installing MAMP on either Operating System.
Note: You can still work with your MongoDB server without a web server. But most applications would require this and that’s why I’m focusing primarily on MongoDB for web development.
You can get some output from the MongoDB process by visiting the localhost address using your installation’s specific port number.
MongoDB will default to 27017. This is the driver port and to view analytics/diagnostics we want to use 28017. So you may access the MongoDB server info on your browser by visiting:
http://localhost:28017
This address should still work properly regardless of your local web server being online or not.
After you have WAMP or MAMP installed and running you can visit the localhost web server on port :80 to see the default page template.
Now I’m going to walk you through installing the PHP driver, and we’ll finish up developing over MongoDB’s PHP class library.
Setup the MongoDB PHP Drivers
Mac and Linux users should be able to install these drivers right from the command line. Looking on the MongoDB PHP language docs we should install using pecl from the Pear Library of PHP code.
Here’s the line of code you should run from terminal:
sudo apt-get install php5-dev php5-cli php-pear sudo pecl install mongo
If you already have Pear installed then you don’t need to run the first line. That is only for PHP installs which are not updated to the latest Pear library. But after the commands finish locate your php.ini file and add the following bit of code:
extension=mongo.so
You should notice a similar 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 a hash symbol(#) are currently active extensions.
After you’ve added this line save & close the file, then restart your Apache web server for the new changes to take effect.
Mongo PHP Extension on Windows
All users on Windows will also need to edit their php.ini file. This can be accomplished directly from the WAMP context menu by clicking on the icon, then moving to PHP -> php.ini. You’ll need to add the same line of code except the filename should be php_mongo.dll.
Also instead of installing through the command line it’s much easier 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 latest release which supports your version of PHP (5.2, 5.3, 5.4) and download the .zip. Once you extract the folder find the extension which matches your version of PHP. In my case I’ll use php_mongo-1.2.12-5.3-vc9.dll and rename this to php_mongo.dll.
Now place this file directly inside your PHP extensions directory located in C:\wamp\bin\php\php5.x\ext\
. If you have this file moved over and the extension line of code added to your php.ini file then everything should be good to go! Restart your web server and open up a phpinfo()
page to view the results.
You can do a CTRL + F search for “mongo” and should find details about the module itself.
Mongo Web Development with PHP
There is so much to discuss when it comes to web development and databases. This is only an introduction tutorial so we won’t be able to touch on many topics including users, authentication, updating objects, multiple 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 created “shows” collection. We can pull all this data out using PHP and display the contents on a webpage. I’m creating a new PHP file in my local server root named shows.php with the following code:
<?php // Config $dbhost = 'localhost'; $dbname = 'test'; // Connect to test database $m = new Mongo("mongodb://$dbhost"); $db = $m->$dbname; // select the collection $collection = $db->shows; // pull a cursor query $cursor = $collection->find(); ?>
What I’m doing is selecting our test database and further accessing the internal shows collection. We can run the find() function on any Mongo collection object to pull out a cursor with all the related internal data.
Now to output this information onto the page let’s use var_dump() which is a much better alternative than print_r(). Add this last block of code directly underneath the $cursor
variable.
foreach($cursor as $document) { var_dump($document); }
This foreach()
loop will go through the cursor results and output variable data for each internal array. We should have 3 objects displaying the data added into our TV Shows earlier. You will notice there is also another key named _id which is the automatic object ID created for each document.
I have to recommend just going through Google or the MongoDB docs to learn more about the PHP class. There is so much information that it cannot all be crammed into this introductory tutorial. But this small PHP script should be an example of just how flexible Mongo databases really are! No confusing SQL commands, no requirements for authentication(unless needed), and all the syntax is very easy to read.
Final Thoughts
Developers who are familiar with databases may still be pushing through this article with difficulty. Going through this tutorial two or three times may still 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 enough to nail down a really good understanding.
The Mongo open source database system is schemaless and quickly scalable in comparison to other rival systems. You are not limited to columns or tables and inserting data can be quickly accomplished through JSON-like syntax. Also, connecting your web applications using PHP is often easier than MySQL/MSSQL once you understand the code.
I do hope this beginner’s tutorial can provide a solid overview from MongoDB terminology to installation, shell commands, and light web development.
Overall Mongo may not be your first database choice when building a new web project. But the system is safe, very reliable, and slowly gaining attention with a growing community of dedicated supporters.
via hongkiat.com http://www.hongkiat.com/blog/webdev-with-mongodb-part3/