0

For connecting Mongodb with PHP application, i installed Mongodb Driver in windows and extension was enabled (checked in phpinfo()). Then i execute the folloeing php code

<?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);  
     }   

     ?> 

and it returns a fatal error. How can solve this?

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
user2198047
  • 9
  • 1
  • 6
  • did you add any data? bcoz you are not define here. You are just performing fetching operation – Mayuri Apr 21 '14 at 13:05
  • http://stackoverflow.com/a/2972662/1433414 You need to download, install, and run MongoDB too. See http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/ – bjori Apr 29 '14 at 21:02

2 Answers2

0

This is connection and test wode. check whether it is working in your system or not :

<?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>";

?>
Mayuri
  • 402
  • 6
  • 13
  • I run this code but the same error occured- 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:2 Stack trace: #0 C:\xampp\htdocs\check\index.php(2): MongoClient->__construct('localhost') #1 {main} thrown in C:\xampp\htdocs\check\index.php on line 2 – user2198047 Apr 22 '14 at 04:53
  • Same connection working in my system perfectly. plz check your code once again or there might be a problem in installation. – Mayuri Apr 22 '14 at 05:05
  • Installation problem means? I check the and its same as you posted earlier. – user2198047 Apr 22 '14 at 05:07
  • In my local PHP version is - 5.3.8, MongoDB - 1.5.1 – user2198047 Apr 22 '14 at 05:08
  • did u insert any data? bcoz u r not mention here – Mayuri Apr 22 '14 at 05:46
  • Now i'm working with your code posted above. No changes. – user2198047 Apr 22 '14 at 05:50
  • How it check? i checked phpinfo and the extension is enabled in my local. – user2198047 Apr 22 '14 at 06:20
0

Have you started mongod server , i could replicate your error only when i closed mongod server .

user2373881
  • 119
  • 1
  • 2
  • 10