skip to Main Content

I had Downloaded and installed the mongo dB And it still showing the error

Please tell me how to configure and connect to MongoDB in PHP

please check the screenshot

3

Answers


  1. <?php
    $client = new MongoDBClient(
        'mongodb+srv://<username>:<password>@<cluster-address>/test?retryWrites=true&w=majority'
    );
    $db = $client->test;
    

    Reference

    Login or Signup to reply.
  2. before you connect you want to add a mongo driver for PHP, then you

    [reference][1]
    [video reference][2]

    <?PHP
        $m = new MongoClient();
        echo "Connection to database successfully";
    ?>
    

    [1]: https://www.tutorialspoint.com/mongodb/mongodb_php.htm#:~:text=%3C%3F-,php%20%2F%2F%20connect%20to%20mongodb%20%24m%20%3D%20new%20MongoClient()%3B%20echo,('%24set'%3D%3E
    [2]: https://www.youtube.com/watch?v=wSIQ8n6H9os

    Login or Signup to reply.
  3. After you completed the installation did you added

    [extension=mongodb.so]
    

    in your php.ini file? If no please add this line of code to your php.ini file.

    Please note that each OS has different steps to install the driver.

    You can find more info about the PHP driver here
    https://www.php.net/manual/en/mongodb.setup.php

    To check if you have installed the driver properly, create a info.php in your project folder and add this function phpinfo(); to your file.

    Visit the info.php file in your browser and try to find the mongodb module there, you can use this anchor #module_mongodb appended to your url to see the module section directly.

    If you can’t find nothing there, it means that your MongoDB/PHP driver is not installed/configured properly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search