dua6992 2017-04-07 06:22
浏览 2081
已采纳

如何解决致命错误:找不到类'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
  • 写回答

2条回答 默认 最新

  • duanjiebian6712 2017-04-07 08:45
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?