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

如何解决致命错误:找不到类'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条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解