Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • PHP Code to connect to MongoDB

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 231
    Comment on it

    To connect MongoDB with PHP you need to install MongoDB php driver. After you successful install MongoDB php driver, you need to create connection with MongoDB.

    MongoDB default port is 27017. To connect with localhost and default port

    $connection = new MongoClient();
    

    If you want to connect other than localhost with default port

    $connection = new MongoClient( "mongodb://abcd.com" );
    

    If you want to connect to host at a different port

    $connection = new MongoClient( "mongodb://abcd.com:53554" );
    

    Above will create successful connection with MongoDB.

    If you want to select database then use the following code.

    $db = $connection->databasename;
    

    If you want to create collection then use the following code.

    $collection = $db->createCollection("students");
    

    Hope above is help you to connect MongoDB with PHP.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: