How to use mongodb with php

If you want to use the mongodb with php then you need to install pecl libraries. Mongo extension is not bundled with PHP.

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.

How to use mongodb with php

How to use mongodb with php
How to use mongodb with php

For installing the Mongo use following URL:
If you dont have pecl installed on your linux machine then use following command.
# yum install php-pecl*
then use following command
# pecl install mongo
Then open the php.ini file. if you are using the linux then use following command.
# vim /etc/php.ini

## Add following lines, end of php.ini file ##
# MongoDB Driver
extension=mongo.so

Restart the apache webserver using following command
# /etc/init.d/httpd restart
If Mongodb server is running then use can test your mongodb database using following code:
Create the mongotest.php file

<!--?php
// connect
$m = new Mongo();
// select a database
$db = $m--->wordpress;
$collection = $db->wordpressapi;
// add an element
$obj = array( "title" => "Sony this Good.\n", "author" => "Wordpressapi.com" );
$collection->insert($obj);
// add another element, with a different "shape"
$obj = array( "title" => "Wordpressapi.com", "online" => true );
$collection->insert($obj);
// find everything in the collection
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $obj) {
echo $obj["title"] . "\n";
echo $obj["author"] . "\n";
}
// disconnect
$m->close();
?>

If you open this file in browser then you can see the following words in browser:
Sony this Good. WordPressapi.com Sony this Good. WordPressapi.com
If you want to use the mongodb with php then you need to install pecl libraries. Mongo extension is not bundled with PHP.
For installing the Mongo use following URL:
If you dont have pecl installed on your linux machine then use following command.# yum install php-pecl*
then use following command# pecl install mongo
Then open the php.ini file. if you are using the linux then use following command.
# vim /etc/php.ini

## Add following lines, end of php.ini file ### MongoDB Driverextension=mongo.so

Restart the apache webserver using following command
# /etc/init.d/httpd restart
If Mongodb server is running then use can test your mongodb database using following code:
Create the mongotest.php file

<!--?php
// connect$m = new Mongo();
// select a database$db = $m--->wordpress;$collection = $db->wordpressapi;
// add an element$obj = array( "title" => "Sony this Good.\n", "author" => "Wordpressapi.com" );$collection->insert($obj);
// add another element, with a different "shape"$obj = array( "title" => "Wordpressapi.com", "online" => true );$collection->insert($obj);
// find everything in the collection$cursor = $collection->find();
// iterate through the resultsforeach ($cursor as $obj) {    echo $obj["title"] . "\n";     echo $obj["author"] . "\n";}
// disconnect$m->close();
?>

If you open this file in browser then you can see the following words in browser:Sony this Good. WordPressapi.com Sony this Good. WordPressapi.com

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

4 thoughts on “How to use mongodb with php”

  1. When I run the above code it shows in me error as below.

    Deprecated: main(): The Mongo class is deprecated, please use the MongoClient class

    Please help!!

    1. The Mongo class, This class extends MongoClient and provides access to several deprecated methods. Instead of this use following.
      Mongo extends MongoClient {}

Leave a Reply

Your email address will not be published.