skip to Main Content

I am using Windows 11, installed Composer, and downloaded the Mongodb dll file.

someone has a better method to connect php with Mongodb.

2

Answers


  1. Chosen as BEST ANSWER
    1. check your php version file at C:laragonbinphpphp-8.1.10-Win32-vs16-x64 / php8ts.dll

    2. download dll file from https://pecl.php.net/package/mongodb/1.13.0/windows based on php8ts.dll file

    3. download 8.1 Thread Safe (TS) x64

    4. you will get a php_mongodb-1.13.0-8.1-ts-vs16-x64.zip

    5. copy php_mongodb.dll file in ext folder of php

    6. C:laragonbinphpphp-8.1.10-Win32-vs16-x64ext

    7. edit C:laragonbinphpphp-8.1.10-Win32-vs16-x64 / php.ini file and add

    8. extension=mongodb

    9. restart server

    10. install composer in windows

    11. open command prompt

    12. goto your project directory www/<your_project>

    13. create composer.json file

    { "require": { "mongodb/mongodb": "^1.12" } }

    1. run command > composer require mongodb/mongodb
    <?php
    require_once 'vendor/autoload.php';
    $client = new MongoDBClient('mongodb://localhost:27017');
    $collection = $client->selectCollection('iitj', 'home');
    $document = $collection->findOne();
    var_dump($document);
    

  2. First, You need to establish a connection to your MongoDB server using the following PHP code:

    <?PHP
    // MongoDB connection parameters
    $mongoDBConnection = new 
    MongoDBDriverManager("mongodb://localhost:27017");
    
    // Database and Collection
    $databaseName = "test_db";
    $collectionName = "test_collection";
    
    // Select database and collection
    $collection = new MongoDBDriverQuery(["test_query_filter"], ["limit" => 10]);
    

    Now, Replace "mongodb://localhost:27017" with your MongoDB connection string. Also, set the appropriate $databaseName and $collectionName variables.

    <?PHP
    // Execute this query
    $cursor = $mongoDBConnection->executeQuery("$databaseName.$collectionName", $collection);
    
    // Loop through the results
    foreach($cursor as $document) {
    var_dump($document);
    // Process each document as needed
    }
    ?>
    

    In the above code, the $cursor variable contains the result set from your MongoDB query. You can iterate through the cursor to access individual documents returned by the query.

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