MongoDB and Php Beginner Tutorials

Database connection: 
Connecting mongoDB with php is really simple if your configured php with mongoDB using php driver. Assuming that you have already configured php with mongoDB am going to explain the php script to connect mongoDB. For configuring mongoDb with php inlinux , you can follow this link. How to Configure Php with mongoDb on Ubuntu

Sample Php web app using mongoDB:

<?php

//connect to mongoDB using mongo class
$m = new Mongo();

//selecting the database, which will be created on the fly
$db = $m->selectDB("comtechies");

//creating or selecting a collection. If the collection is already there it will be selected or it will be created on the fly.

$collection = $db->comtechies->demo;

//creating a document with array
$doc = array(
"name" => "bibin",
"place" => "kollemcode",
"position" => "admin"
);

//inserting the document in to collection
$collection->insert($doc);

//store the first row in the collection to the variable result
$result = $collection->findone();

//shows the result
var_dump($result);


?>

Other Interesting Blogs

Leave a Comment

Share via
Copy link
Powered by Social Snap