O'Reilly logo

PHP and MongoDB Web Development by Rubayeet Islam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - retrieving articles from a database

In this example, we are going to build the home page of our blog site, which will show the title and the content excerpt of every article stored in the database. Clicking on an article will take us to another page where we can see the full content of the article. Let's get to it:

  1. Open up the text editor and create a new file named blogs.php. Put the following code in that file:
    <?php try { $connection = new Mongo(); $database = $connection->selectDB('myblogsite'); $collection = $database->selectCollection('articles'); } catch(MongoConnectionException $e) { die("Failed to connect to database ".$e->getMessage()); } $cursor = $collection->find(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required