1

I have connected my php web app to mongodb with the help of codeigniter driver.

now i am using the mongoo_db class to insert a collection into the default database, db_name.

the source code of my files is,

ghy.php

<?php
/**
* @author
* @copyright 2014
*/
class ghy
{
    public $id;
    public $name;
    public function insert($collection = "", $data = array()) {
        if(empty($collection))
            show_error("No Mongo collection selected to insert into", 500);
        if(count($data) == 0 || !is_array($data))
            show_error("Nothing to insert into Mongo collection or insert is not an array", 500);
        try {
            $this->db->{$collection}->insert($insert, array('safe' => TRUE));
            if(isset($insert['_id']))
                return($insert['_id']);
            else
                return(FALSE);
        } catch(MongoCursorException $e) {
            show_error("Insert of data into MongoDB failed: {$e->getMessage()}", 500);
        }
        $use=$this->mongo_db->insert('foo', $data = array('4','hgfh'));
        var_dump();
        }
    }
?>

the problem is that i am not getting any output in my browser.

can anyone plss explain me where am i going wrong? replies at the the earliest will be highly appreciated.

thankyou

user3189732
  • 11
  • 1
  • 4

1 Answers1

0

I'm not sure why your class isn't working, but to get an understanding of how MongoDB and Codeigniter works together take a look at this answer.

From the answer to answer your question on how to establish a connection to MongoDB:

config/mongo.php

$config['mongo_server'] = null;
$config['mongo_dbname'] = 'mydb';

libraries/Mongo.php

class CI_Mongo extends Mongo
{
    var $db;

    function CI_Mongo()
    {   
        // Fetch CodeIgniter instance
        $ci = get_instance();
        // Load Mongo configuration file
        $ci->load->config('mongo');

        // Fetch Mongo server and database configuration
        $server = $ci->config->item('mongo_server');
        $dbname = $ci->config->item('mongo_dbname');

        // Initialise Mongo
        if ($server)
        {
            parent::__construct($server);
        }
        else
        {
            parent::__construct();
        }
        $this->db = $this->$dbname;
    }
}

And a sample controller

controllers/posts.php

class Posts extends Controller
{
    function Posts()
    {
        parent::Controller();
    }

    function index()
    {
        $posts = $this->mongo->db->posts->find();

        foreach ($posts as $id => $post)
        {
            var_dump($id);
            var_dump($post);
        }
    }

    function create()
    {
        $post = array('title' => 'Test post');
        $this->mongo->db->posts->insert($post);
        var_dump($post);
    }
}

From the question as above answer:

MongoDB is very well supported within CodeIgniter community, take the time and dive in :p

I hope this helps you to get your Codeigniter(MongoDB) project working

Community
  • 1
  • 1
Renier
  • 1,523
  • 4
  • 32
  • 60
  • i have already seen this.but i didnt understand where to implement functions.. The answer u gave is about drivers – user3189732 Jan 15 '14 at 14:27
  • My answer is on how to use MongoDB libraries and you asked in the comments above `how to use active driver library` you just need to take this code implement it in your project and you will be able to connect to your MongoDB database. you implement it in the files indicated above the code, i.e. `config/mongo.php` means that in your `config` folder you would have a file called `mongo.php`, if it does not exist, then create it. – Renier Jan 15 '14 at 14:38
  • thankyou i have already done that .now wen i am using in my php code it aint unable to connect to database – user3189732 Jan 16 '14 at 04:24