如何解决致命错误:找不到类'MongoClient'?
I am using windows 10 64 bit, xampp 3.2.2, PHP 5.6.30, PHP Extension Build VC11, MongoDB server version: 3.4.3.
I am getting error like "Fatal error: Class 'MongoClient' not found in D:\xampp\htdocs\test\test1.php on line 4".
Here is the code I am using
CODE
<?php
// connect
$m = new MongoClient();
// select a database
$db = $m->cabin;
// select a collection (analogous to a relational database's table)
$collection = $db->user;
// find everything in the collection
$cursor = $collection->find();
// iterate through the results
foreach ($cursor as $document) {
echo $document["title"] . "
";
}
?>
I have added dll file in folder (D:\xampp\php\ext)
and added extension in php.ini (D:\xampp\php)
"extension=php_mongodb.dll". But issue is not solved.
MongoDB (Mongo db is working)
user@DESKTOP-JCDJQ65 MINGW64 /d
$ mongo
MongoDB shell version v3.4.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.3
use cabin
switched to db cabin
show tables
bmessages
booking
cabins
club
country
customer
email
facilities
land
mschool
region
role
rooms
settings
tempuser
tour
user
userold
visit
我使用的是Windows 10 64位,xampp 3.2.2,PHP 5.6.30,PHP Extension Build VC11,MongoDB 服务器版本:3.4.3。 p>
我收到的错误如“第4行的D:\ xampp \ htdocs \ test \ test1.php中找不到致命错误:类'MongoClient'” 。 p>
以下是我正在使用的代码 p>
代码 strong> p>
我在文件夹 MongoDB strong>(Mongo数据库工作正常) p>
&lt;?php
//连接
$ m = new MongoClient();
//选择数据库
$ db = $ m-&gt; cabin;
// 选择一个集合(类似于关系数据库的表)
$ collection = $ db-&gt; user;
//找到集合中的所有内容
$ cursor = $ collection-&gt; find();
//遍历结果
foreach($ cursor as $ document){
echo $ document [“title”]。 “
”;
}
?&gt;
code> pre>
中添加了dll文件(D:\ xampp \ php \ ext) code>并在php.ini
(D:\ xampp \ php) code>“extension = php_mongodb.dll”中添加了扩展名。 但是问题没有解决。 p>
user @ DESKTOP-JCDJQ65 MINGW64 / d
$ mongo
MongoDB shell版本v3.4.3
连接到:mongodb://127.0.0.1:27017
MongoDB服务器版本:3.4.3
use cabin
switched到db cabin
show tables
bmessages
booking
cabins
club
country
customer
email
facilities
land
mschool
region
role
rooms
settings
mpmpuser
tour
user
userold
visit
code> pre>
div>
MongoDB\Driver\Manager is responsible for maintaining connections to MongoDB.
Connection
$mng = new MongoDB\Driver\Manager("mongodb://localhost:27017");
Listing databases
$listdatabases = new MongoDB\Driver\Command(["listDatabases" => 1]);
$res = $mng->executeCommand("admin", $listdatabases);
Reading All data
$query = new MongoDB\Driver\Query([]);
$rows = $mng->executeQuery("database_name.collection_name", $query);
foreach ($rows as $row) {
echo "$row->name
";
}
Bulk Write(to perform two or more operations simultanously)
$bulk = new MongoDB\Driver\BulkWrite;
$doc = ['_id' => new MongoDB\BSON\ObjectID, 'name' => 'Toyota', 'price' => 26700];
$bulk->insert($doc);
$bulk->update(['name' => 'Audi'], ['$set' => ['price' => 52000]]);
$bulk->delete(['name' => 'Hummer']);
$mng->executeBulkWrite('testdb.cars', $bulk);
MongoClient
belongs to long deprecated Mongo extension. The new MongoDB extension uses Manager to connect to the database, e.g.
$m = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$cursor = $manager->executeQuery("cabin.user", new MongoDB\Driver\Query([]));
foreach ($cursor as $document) {
echo $document["title"] . "
";
}
Or use any of higher level abstraction libraries. E.g. https://github.com/mongodb/mongo-php-library provides interface similar to the legacy driver.