php 找不到类“MongoClient”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24533938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:21:19  来源:igfitidea点击:

Class 'MongoClient' not found

phpmongodbconnect

提问by Miguel Cantó Bataller

I'm trying to make this code run:

我正在尝试运行此代码:

<?php

$m = new MongoClient("mongodb://54.72.237.242"); 
$db = $m->tilbud; 

?>

Everytime I got the same error:

每次我遇到同样的错误:

Fatal error: Class 'MongoClient' not found in C:\xampp\htdocs\conexion.php

I've been reading about this problem the whole day but anything solves my issue (I guess it's something I'm doing wrong).

我一整天都在阅读这个问题,但任何东西都可以解决我的问题(我想这是我做错了)。

I downloaded the php_mongo.dll, I copied it in xampp/php/ext and I added extension=php_mongo.dllin the php.ini archive.

我下载了 php_mongo.dll,将它复制到 xampp/php/ext 中,然后添加extension=php_mongo.dll到 php.ini 存档中。

I've added 4 more dll's because I'm not sure which one I have to use:

我又添加了 4 个 dll,因为我不确定必须使用哪一个:

  • extension=php_mongo-1.5.4-5.5-vc11-nts
  • extension=php_mongo-1.5.4-5.5-vc11
  • extension=php_mongo-1.5.4-5.5-vc11-nts-x86_64
  • extension=php_mongo-1.5.4-5.5-vc11-x86_64
  • extension=php_mongo-1.5.4-5.5-vc11-nts
  • extension=php_mongo-1.5.4-5.5-vc11
  • extension=php_mongo-1.5.4-5.5-vc11-nts-x86_64
  • extension=php_mongo-1.5.4-5.5-vc11-x86_64

So now im getting 5 warnings instead of one. At the end I guess one of them will work and I'll delete the other 4.

所以现在我收到了 5 个警告而不是一个。最后,我想其中一个会起作用,我会删除其他 4 个。

Things I tried and I'm sure they are ok:

我尝试过的事情,我确定它们没问题:

  • The extension_dir is pointing to the correct folder.
  • The php.ini that I modified is the one that xammp loads.
  • Phpinfo dosen't show anything about mongo.
  • extension_dir 指向正确的文件夹。
  • 我修改的 php.ini 就是 xammp 加载的那个。
  • Phpinfo 没有显示任何关于 mongo 的信息。

What more can I try ?

我还能尝试什么?

Edit

编辑

I tried

我试过

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

echo extension_loaded("mongo") ?"已加载\n" : "未加载\n";

and it always says 'not loaded'.

它总是说“未加载”。

Edit

编辑

Finally! The problem was the dll's name. It has to be 'php_mongo.dll' and I was trying to load the full name dll as I said at the begining of this post. So I changed the correct dll for me (extension=php_mongo-1.5.4-5.5-vc11) for extension=php_mongo.dlland voilà!

最后!问题是dll的名称。它必须是“php_mongo.dll”,我试图加载全名 dll,正如我在本文开头所说的那样。所以我为我更改了正确的 dll ( extension=php_mongo-1.5.4-5.5-vc11) for extension=php_mongo.dlland voilà!

回答by Lukas Liesis

You have not installed MongoDB PHP driver please see this link http://www.php.net/manual/en/mongo.installation.php

您尚未安装 MongoDB PHP 驱动程序,请参阅此链接http://www.php.net/manual/en/mongo.installation.php

Update sources

更新来源

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update

Install MongoDB PHP Driver

安装 MongoDB PHP 驱动程序

sudo apt-get install php5-dev php5-cli php-pear -y
sudo pecl install mongo

Open your php.ini file and add to it:

打开你的 php.ini 文件并添加:

extension=mongo.so

Restart apache

重启apache

sudo /etc/init.d/apache2 restart

Other helping info:

其他帮助信息:

this should help to find your php.ini file:

这应该有助于找到您的 php.ini 文件:

php -i | grep 'Configuration File'

On Ubuntu it shows this:

在 Ubuntu 上,它显示了这一点:

Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini

take a note, that you run this command from cli (command line) so for your true php.ini go to folder apache2 instead of cli :)

请注意,您是从 cli(命令行)运行此命令的,因此对于真正的 php.ini,请转到文件夹 apache2 而不是 cli :)

回答by shadi

For those arriving on this page with PHP 7 installed:

对于那些安装了 PHP 7 到达此页面的人:

The MongoCLientclass was provided by pecl install mongo. But pecl/mongois not available for php7 and deprecated in favor of pecl/mongodb. But with pecl/mongodbyou'll need to use MongoDB\Driver\Managerinstead of MongoClient(warning on page says so too).

MongoCLient课程由pecl install mongo. 但pecl/mongo不适用于 php7 并且不推荐使用pecl/mongodb. 但是pecl/mongodb你需要使用MongoDB\Driver\Manager而不是MongoClient(页面上的警告也是如此)。

See herefor further reading.

请参阅此处进一步阅读。

This said, you will need an abstraction layer on top of the PHP MongoDB\Driver\Manager. This is provided by mongodb/mongo-php-library.

这就是说,您将需要在 PHP 之上的抽象层MongoDB\Driver\Manager。这是由mongodb/mongo-php-library 提供的

You will need to refactor stuff like:

您将需要重构以下内容:

  • \MongoClientto \MongoDB\Client
  • \MongoCollectionto \MongoDB\Collection
  • \MongoClient->selectDBto \MongoDB\Client->selectDatabase
  • \MongoClient->listDBsto \MongoDB\Client->listDatabases
    • also output is not an array but an iterator, so you'll need to use iterator_to_array, along with edits to how you use the resulting object
  • \MongoCollection->getNameto \MongoDB\Collection->getCollectionName
  • \MongoCollection->updateto \MongoDB\Collection->updateOneor updateMany
  • \MongoCollection->removeto \MongoDB\Collection->deleteOne
  • \MongoCollection->batchInsertto \MongoDB\Collection->insertMany
  • \MongoClient\MongoDB\Client
  • \MongoCollection\MongoDB\Collection
  • \MongoClient->selectDB\MongoDB\Client->selectDatabase
  • \MongoClient->listDBs\MongoDB\Client->listDatabases
    • 输出也不是数组而是迭代器,因此您需要使用iterator_to_array,以及对如何使用结果对象的编辑
  • \MongoCollection->getName\MongoDB\Collection->getCollectionName
  • \MongoCollection->update\MongoDB\Collection->updateOneupdateMany
  • \MongoCollection->remove\MongoDB\Collection->deleteOne
  • \MongoCollection->batchInsert\MongoDB\Collection->insertMany

回答by mcmacerson

The answer is indeed to follow the instructions. I was missing the very important requireline that must come before creating the new mongodb object:

答案确实是按照说明操作。我错过了在创建新的 mongodb 对象之前必须出现的非常重要的require行:

<?php

require 'vendor/autoload.php';

$client = new MongoDB\Client("mongodb://localhost:27017");

And of course you need to run this command in the root of your project as per the instructions:

当然,您需要按照说明在项目的根目录中运行此命令:

composer require mongodb/mongodb

回答by miguelhernandezramos

install the driver, I have for example php5.6:

安装驱动程序,我有例如php5.6:

sudo apt-get install php5.6-mongo

回答by Biswajit Panday

Getting the same error and now it's solved.
I am using Linux Mint. To solve this issue I added extension=mongo.soin two directories:

遇到同样的错误,现在解决了。
我正在使用 Linux Mint。为了解决这个问题,我添加extension=mongo.so了两个目录:

  • /etc/php5/cli/php.ini
  • /etc/php5/apache2/php.ini
  • /etc/php5/cli/php.ini
  • /etc/php5/apache2/php.ini

回答by M D P

From thispage:

这个页面:

Note: Additional DLL dependencies for Windows Users:

注意:Windows 用户的其他 DLL 依赖项:

In order for this extension to work, there are DLL files that must be available to the Windows system PATH

为了使此扩展工作,必须有一些 DLL 文件可用于 Windows 系统 PATH

回答by user3427015

your php version and dll file version should be same check it if versions are not same then update your xampp php according to the available dll version.

您的 php 版本和 dll 文件版本应该相同,如果版本不同,请检查它,然后根据可用的 dll 版本更新您的 xampp php。