Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb PHP应用程序连接_Php_Mongodb - Fatal编程技术网

Mongodb PHP应用程序连接

Mongodb PHP应用程序连接,php,mongodb,Php,Mongodb,为了将Mongodb与PHP应用程序连接,我在windows中安装了Mongodb驱动程序,并启用了扩展(签入phpinfo())。然后我执行下面的php代码 <?php // Config $dbhost = 'localhost'; $dbname = 'test'; // Connect to test database $m = new Mongo("mongodb://$dbhost");

为了将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);  
     }   

     ?> 

这是连接和测试工作。检查它是否在您的系统中工作:

<?php
$m = new MongoClient("localhost");
$dbname = $m->selectDB("Test");
$collection = $dbname->selectCollection("Shows"); 

$data1 = array(
        'user_id' => 'abc',
        'age' => 20,
        'Status' => 'A'
        );

$dbname = $collection->insert($data1, array('$data1' => 1));  


$results = $collection->find();

echo "<pre>";
foreach($results as $test) {
    print_r($test);
} 
echo "</pre>";

?>


您是否启动了mongod服务器,我只能在关闭mongod服务器时复制您的错误。

您添加了任何数据吗?bcoz您在这里没有定义。您正在执行获取操作,需要下载、安装和运行MongoDB。请参阅我运行此代码,但发生了相同的错误-致命错误:未捕获异常“MongoConnectionException”,消息为“未能连接到:localhost:27017:无法建立连接,因为目标计算机主动拒绝它。”在C:\xampp\htdocs\check\index.php:2堆栈跟踪:#0 C:\xampp\htdocs\check\index.php(2):MongoClient->u构造('localhost')#1{main}中,在C:\xampp\htdocs\check\index.php的第2行上抛出,在我的系统中,同一个连接工作得非常好。请再次检查您的代码,否则安装过程中可能会出现问题。安装问题意味着什么?我检查了,它和你之前发布的一样。在我的本地PHP版本是-5.3.8,MongoDB-1.5.1,你是否插入了任何数据?bcoz u r这里没有提到如何启动mongodb服务器?我认为您只安装了mongodb驱动程序,而没有安装mongodb服务器本身,因此让mongodb.org下载mongodb for windows解压下载并运行mongod.exe来启动mongodb服务器
<?php
$m = new MongoClient("localhost");
$dbname = $m->selectDB("Test");
$collection = $dbname->selectCollection("Shows"); 

$data1 = array(
        'user_id' => 'abc',
        'age' => 20,
        'Status' => 'A'
        );

$dbname = $collection->insert($data1, array('$data1' => 1));  


$results = $collection->find();

echo "<pre>";
foreach($results as $test) {
    print_r($test);
} 
echo "</pre>";

?>