Installation
Windows
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/
You can download PHP mongo drive/extension dll file from following URL
https://github.com/mongodb/mongo-php-driver/downloads. Copy
php_mongo.dll file into your PHP extension directory
Linux & Mac
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
Connecting into MongoDB
PHP code connecting with out Authentication.
<?php
$mongo = new Mongo();
$db = $mongo->selectDB(“test”);
?>
With Authentication
<?php
$mongo = new Mongo(“mongodb://{$username}:{$password}@{$host}”);
$db = $mongo->selectDB(“test”);
?>
By default MongoDB contains sample database called “
test”.
Creating New Database
$db = $mongo->Database_Name;
Note: Technically, you don't need to manually create databases in MongoDB due to its schemaless "lazy" way of creating databases.
Queries
PHP code to get MongoDB databases.
//Get list of databases
$mongo->admin->command(array(“listDatabases” => 1));
//Get list of Collections in test db
$db->listCollections();
Mongo Shell/Terminal/Command Prompt
db.listDatabases
db.test.showCollections
Create Collection(table)
You can created a collection/table but executing following code.
$db->createCollection(“people”,false);
Note: here false referred to unlimitted size, if you give true you have to mention the size of maximum.
Mongo Shell
db.createCollection(“people”,false);
Insert Record
Insert records int MongoDB.
<?php
$people = $db->people;
$insert = array(“user” => “demo@9lessons.info”, “password” => md5(“demo_password”));
$db->insert($insert);
?>
Mongo Shell:
db.people.insert({user:”user_name”,password:”password”});
Update Records
Update records into MongoDB
<?php
$update = array(“$set” => array(“user” => “demo@9lessons.info”));
$where = array(“password” => “password”);
$people->update($where,$update);
?>
Mongo Shell:
db.people.update({password:”password”},{$set : {user:”demo@demo.com”}});
HTML Code
<form action="index.php" method="POST">
Email:
<input type="text" id="usr_email" name="usr_email" />
Password:
<input type="password" id="usr_password" name="usr_password" />
<input name="submitForm" id="submitForm" type="submit" value="Login" />
</form>
PHP Code
<?php
$succss = "";
if(isset($_POST) and $_POST['submitForm'] == "Login" )
{
$usr_email = mysql_escape_string($_POST['usr_email']);
$usr_password = mysql_escape_string($_POST['usr_password']);
$error = array();
// Email Validation
if(empty($usr_email) or !filter_var($usr_email,FILTER_SANITIZE_EMAIL))
{
$error[] = "Empty or invalid email address";
}
if(empty($usr_password)){
$error[] = "Enter your password";
}
if(count($error) == 0){
$con = new Mongo();
if($con){
// Select Database
$db = $con->test;
// Select Collection
$people = $db->people;
$qry = array("user" => $usr_email,"password" => md5($usr_password));
$result = $people->findOne($qry);
if($result){
$success = "You are successully loggedIn";
// Rest of code up to you....
}
} else {
die("Mongo DB not installed");
}
}
}
?>