MongoDB opens up a whole new level of opportunities for the PHP developer. Once you have the Mongo database setup and the Mongo PHP driver installed and activated, you need to write code to make a connection to the Mongo database. In PHP connecting to Mongo is as simple as connecting to a MySQL server.
To connect to the Mongo database, you need the URL (or IP Address) and a port.
First setup your connection string like this:
$url = 'localhost';
$port = 27017;
$connStr = $url.':'.$port;
If you have security enabled on Mongo, next add the authentication information:
$auth = empty($username)   ?   ''   :   "mongodb://{$username}:{$password}@";Finally, make the connection:
$myConnection = new Mongo($auth.$connStr);
If you want to handle connection problems, wrap the connection in a try...catch block:
try {Â Â Â $myConnection = new Mongo($auth.$connStr);
} catch (Exception $e) {Â Â echo $e->getMessage();
}
No you have a connection to your Mongo database!



Del.ici.ous


