【发布时间】:2023-03-08 18:53:02
【问题描述】:
为了将 Mongodb 与 PHP 应用程序连接,我在 windows 中安装了 Mongodb 驱动程序并启用了扩展(在 phpinfo() 中检查)。然后我执行以下 php 代码
<?php
// Config
$dbhost = 'localhost';
$dbname = 'test';
// Connect to test database
$m = new Mongo("mongodb://$dbhost");
$db = $m->$dbname;
// select the collection
$collection = $db->shows;
// pull a cursor query
$cursor = $collection->find();
foreach($cursor as $document) {
var_dump($document);
}
?>
它返回一个致命错误。怎么解决?
Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: localhost:27017: No connection could be made because the target machine actively refused it. ' in C:\xampp\htdocs\check\index.php:7 Stack trace: #0 C:\xampp\htdocs\check\index.php(7): MongoClient->__construct('mongodb://local...') #1 {main} thrown in C:\xampp\htdocs\check\index.php on line 7
【问题讨论】:
-
您添加了任何数据吗? bcoz 你没有在这里定义。你只是在执行抓取操作