找不到类"MongoDB \ Client",已安装mongodb扩展 [英] Class 'MongoDB\Client' not found, mongodb extension installed

查看:499
本文介绍了找不到类"MongoDB \ Client",已安装mongodb扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建执行以下代码的新mongo连接

I tried to create new mongo connection executing the following code

$m = new MongoDB\Client();

我收到此错误:

致命错误:找不到类'MongoDB \ Client'

Fatal error: Class 'MongoDB\Client' not found

我认为我已经正确安装了MongoDB扩展 (将php_mongodb.dll复制到ext文件夹,并用 extension = php_mongodb.dll 更新php.ini).

i think i have properly installed MongoDB extension (Copied php_mongodb.dll to ext folder and updated php.ini with extension=php_mongodb.dll).

以下代码确认已加载:

echo extension_loaded("mongodb") ? "loaded\n" : "not loaded\n";

我仍然收到相同的错误.

I still receive the same error.

这里是 phpinfo()

感谢您的帮助.谢谢!

推荐答案

如果您使用的是最新的MongoDB PHP扩展,请

If you are using latest MongoDB extension of PHP, MongoDB\Driver\Manager is the main entry point to the extension.

这是使用最新扩展名检索数据的示例代码.

Here is the sample code to retrieve data using latest extension.

假设您在testDb中具有testColl集合.您可以使用扩展的 MongoDB\Driver\Query 类检索数据

Let's say you have testColl collection in testDb. The you can retrieve data using MongoDB\Driver\Query class of the extension.

// Manager Class
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");

// Query Class
$query = new MongoDB\Driver\Query(array('age' => 30));

// Output of the executeQuery will be object of MongoDB\Driver\Cursor class
$cursor = $manager->executeQuery('testDb.testColl', $query);

// Convert cursor to Array and print result
print_r($cursor->toArray());

输出:

Array
(
    [0] => stdClass Object
        (
            [_id] => MongoDB\BSON\ObjectID Object
                (
                    [oid] => 5848f1394cea9483b430d5d2
                )

            [name] => XXXX
            [age] => 30
        )

)

这篇关于找不到类"MongoDB \ Client",已安装mongodb扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆