Search code examples
phpmongodbxampppostman

how to solve fatal error : Uncaught Error?


I did REST API MONGODB-PHP CRUD via postman and got an uncaught error. I use XAMPP 3.2.2 PHP 7.2.1 are there any suggestion to fix it ?

db.php

<?php
class DbManager {

    //Database configuration
    private $dbhost = 'localhost';
    private $dbport = '27017';
    private $conn;

    function __construct(){
        //Connecting to MongoDB
        try{
            //Establish database connection
            $this->conn = new MongoDBDriverManager('mongodb://'.$this->dbhost.':'.$this->dbport);
        }catch (MongoDBDriverExceptionException $e) {
            echo $e->getMessage();
            echo nl2br("n");
        }
    }

    function getConnection() {
        return $this->conn;
    }
}
?>

how i fix error ?

<br />
<b>Fatal error</b>:  Uncaught Error: Class 'MongoDBDriverManager' not found in C:\xampp\htdocs\mongo\db.php: 13
Stack trace:
#0 C:\xampp\htdocs\mongo\create.php(17): DbManager-&gt;__construct()
#1 {main
}
  thrown in <b>C:\xampp\htdocs\mongo\db.php</b> on line <b>13</b><br />

Solution

  • You need rename

    $this->conn = new MongoDBDriverManager('mongodb://'.$this->dbhost.':'.$this->dbport);
    

    with

    $this->conn = new MongoDB\Driver\Manager('mongodb://'.$this->dbhost.':'.$this->dbport);
    

    See here