| How to connect MongoDB from php code?
Below is the code to connect MongoDB database from PHP. And if database is not there with that name it will automatically create that database in MongoDB. Below is the syntax
< ?php
// connect to mongodb with below code
$m = new MongoClient();
echo "First step Connected to database";
// select a database with given below command
$db = $m->mytestdb;
echo "Database mytestdb selected";
?>
after executing above code in php we will get below two lines as output:-
First step Connected to database
Database mytestdb selected | | |