search
HomeBackend DevelopmentPHP Tutorialphp Class ‘MongoClient’ NOT Found

windows下为php7.0.4安装目前官方版本对应的最新的php_mongodb.dll扩展,该扩展版本为1.1.6,支持最高版本MongoDB3.2,支持php7.0,;在phpinfo中可以看到MongoDB扩展,如图
但是在没有使用命名空间的情况下,new MongoClient();出现如下错误
Fatal error: Uncaught Error: Class 'MongoClient' not found in E:\.......... 这是为什么??请问

版本支持如下图(注意mongodb1.1那一行)
1、扩展对MOngoDB版本的支持


2、扩展对php版本的支持


回复讨论(解决方案)

没用过,不好说

不过你怎么知道就一定有 MongoClient 类呢?
你至少需要
print_r(get_extension_funcs('mongodb'));
看一下该扩展中提供了那些函数
print_r(get_declared_classes());
看一下预定义类中是否有你需要(或相似)的类名

php.net的官方文档是这么用的new MongoClient()
我也不清楚是否有这类,是不是我没有安装对应的VC?
vc14的下载地址是什么,怎么在网上找不到

@xuzuning 

我不是让你看一下吗?
手边的事情就那么难?

Array
(
    [0] => MongoDB\BSON\fromPHP
    [1] => MongoDB\BSON\toPHP
    [2] => MongoDB\BSON\toJSON
    [3] => MongoDB\BSON\fromJSON
)
Array
(
    [0] => stdClass
    [1] => Exception
    [2] => ErrorException
    [3] => Error
    [4] => ParseError
    [5] => TypeError
    [6] => ArithmeticError
    [7] => DivisionByZeroError
    [8] => Closure
    [9] => Generator
    [10] => ClosedGeneratorException
    [11] => DateTime
    [12] => DateTimeImmutable
    [13] => DateTimeZone
    [14] => DateInterval
    [15] => DatePeriod
    [16] => LogicException
    [17] => BadFunctionCallException
    [18] => BadMethodCallException
    [19] => DomainException
    [20] => InvalidArgumentException
    [21] => LengthException
    [22] => OutOfRangeException
    [23] => RuntimeException
    [24] => OutOfBoundsException
    [25] => OverflowException
    [26] => RangeException
    [27] => UnderflowException
    [28] => UnexpectedValueException
    [29] => RecursiveIteratorIterator
    [30] => IteratorIterator
    [31] => FilterIterator
    [32] => RecursiveFilterIterator
    [33] => CallbackFilterIterator
    [34] => RecursiveCallbackFilterIterator
    [35] => ParentIterator
    [36] => LimitIterator
    [37] => CachingIterator
    [38] => RecursiveCachingIterator
    [39] => NoRewindIterator
    [40] => AppendIterator
    [41] => InfiniteIterator
    [42] => RegexIterator
    [43] => RecursiveRegexIterator
    [44] => EmptyIterator
    [45] => RecursiveTreeIterator
    [46] => ArrayObject
    [47] => ArrayIterator
    [48] => RecursiveArrayIterator
    [49] => SplFileInfo
    [50] => DirectoryIterator
    [51] => FilesystemIterator
    [52] => RecursiveDirectoryIterator
    [53] => GlobIterator
    [54] => SplFileObject
    [55] => SplTempFileObject
    [56] => SplDoublyLinkedList
    [57] => SplQueue
    [58] => SplStack
    [59] => SplHeap
    [60] => SplMinHeap
    [61] => SplMaxHeap
    [62] => SplPriorityQueue
    [63] => SplFixedArray
    [64] => SplObjectStorage
    [65] => MultipleIterator
    [66] => ReflectionException
    [67] => Reflection
    [68] => ReflectionFunctionAbstract
    [69] => ReflectionFunction
    [70] => ReflectionGenerator
    [71] => ReflectionParameter
    [72] => ReflectionType
    [73] => ReflectionMethod
    [74] => ReflectionClass
    [75] => ReflectionObject
    [76] => ReflectionProperty
    [77] => ReflectionExtension
    [78] => ReflectionZendExtension
    [79] => SessionHandler
    [80] => __PHP_Incomplete_Class
    [81] => php_user_filter
    [82] => Directory
    [83] => AssertionError
    [84] => ZipArchive
    [85] => LibXMLError
    [86] => DOMException
    [87] => DOMStringList
    [88] => DOMNameList
    [89] => DOMImplementationList
    [90] => DOMImplementationSource
    [91] => DOMImplementation
    [92] => DOMNode
    [93] => DOMNameSpaceNode
    [94] => DOMDocumentFragment
    [95] => DOMDocument
    [96] => DOMNodeList
    [97] => DOMNamedNodeMap
    [98] => DOMCharacterData
    [99] => DOMAttr
    [100] => DOMElement
    [101] => DOMText
    [102] => DOMComment
    [103] => DOMTypeinfo
    [104] => DOMUserDataHandler
    [105] => DOMDomError
    [106] => DOMErrorHandler
    [107] => DOMLocator
    [108] => DOMConfiguration
    [109] => DOMCdataSection
    [110] => DOMDocumentType
    [111] => DOMNotation
    [112] => DOMEntity
    [113] => DOMEntityReference
    [114] => DOMProcessingInstruction
    [115] => DOMStringExtend
    [116] => DOMXPath
    [117] => PDOException
    [118] => PDO
    [119] => PDOStatement
    [120] => PDORow
    [121] => SimpleXMLElement
    [122] => SimpleXMLIterator
    [123] => XMLReader
    [124] => XMLWriter
    [125] => PharException
    [126] => Phar
    [127] => PharData
    [128] => PharFileInfo
    [129] => CURLFile
    [130] => finfo
    [131] => MongoDB\Driver\Command
    [132] => MongoDB\Driver\Cursor
    [133] => MongoDB\Driver\CursorId
    [134] => MongoDB\Driver\Manager
    [135] => MongoDB\Driver\Query
    [136] => MongoDB\Driver\ReadConcern
    [137] => MongoDB\Driver\ReadPreference
    [138] => MongoDB\Driver\Server
    [139] => MongoDB\Driver\BulkWrite
    [140] => MongoDB\Driver\WriteConcern
    [141] => MongoDB\Driver\WriteConcernError
    [142] => MongoDB\Driver\WriteError
    [143] => MongoDB\Driver\WriteResult
    [144] => MongoDB\Driver\Exception\LogicException
    [145] => MongoDB\Driver\Exception\RuntimeException
    [146] => MongoDB\Driver\Exception\UnexpectedValueException
    [147] => MongoDB\Driver\Exception\InvalidArgumentException
    [148] => MongoDB\Driver\Exception\ConnectionException
    [149] => MongoDB\Driver\Exception\AuthenticationException
    [150] => MongoDB\Driver\Exception\SSLConnectionException
    [151] => MongoDB\Driver\Exception\WriteException
    [152] => MongoDB\Driver\Exception\BulkWriteException
    [153] => MongoDB\Driver\Exception\ExecutionTimeoutException
    [154] => MongoDB\Driver\Exception\ConnectionTimeoutException
    [155] => MongoDB\BSON\Binary
    [156] => MongoDB\BSON\Javascript
    [157] => MongoDB\BSON\MaxKey
    [158] => MongoDB\BSON\MinKey
    [159] => MongoDB\BSON\ObjectID
    [160] => MongoDB\BSON\Regex
    [161] => MongoDB\BSON\Timestamp
    [162] => MongoDB\BSON\UTCDateTime
    [163] => mysqli_sql_exception
    [164] => mysqli_driver
    [165] => mysqli
    [166] => mysqli_warning
    [167] => mysqli_result
    [168] => mysqli_stmt
    [169] => SoapClient
    [170] => SoapVar
    [171] => SoapServer
    [172] => SoapFault
    [173] => SoapParam
    [174] => SoapHeader
    [175] => SQLite3
    [176] => SQLite3Stmt
    [177] => SQLite3Result
    [178] => XSLTProcessor
)
与MongoDB有关的就这几个而已

找到原因了,是因为旧版的php_mongo.dll才是使用MongoClient类来实现连接,新版的php_mongodb.dll是使用 MongoDB\Driver\Manager来连接的;注意该类命名空间不能省略。
这是旧版的参考文档 http://php.net/manual/zh/book.mongo.php
这是新版的参考文档 http://php.net/manual/zh/book.mongodb.php
下面是新版的使用方法:

	$con    = new MongoDB\Driver\Manager('mongodb://127.0.0.1:27017');	$query  = new MongoDB\Driver\Query([]);	$cursor = $con->executeQuery('databaseName.collectionName',$query);	$it     = new IteratorIterator($cursor);	$it->rewind();	while ($doc=$it->current()) {		print_r($doc);		$it->next();		echo '<br/>';	}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools