简体   繁体   中英

PHP cannot connect to MongoDB

Recently I upgraded my Wamp server from Wamp 2.5 to Wamp 3.0 succesfully. Now, in my Wamp 3.0 server, PHP is at version 5.6.31.

I downloaded the MongoDB driver (stable version 1.2.10) from https://pecl.php.net/package/mongodb , and installed it successfully into my Wamp server. The server shows mongodb as one of the loaded extensions.

I encountered an error message: Fatal error: Class 'MongoClient' not found in C:\\wamp64\\www\\loginNew.php on line ...

That PHP code ran perfectly in the previous version of Wamp (and PHP). Here is the code

// connect to mongodb
$m = new MongoClient();

// select a database
$db = $m->ConsentForm;

$collection = $db->ConsentFormDetail;     

$agreeBoolean           = $_POST['agree']; 
$nameInitial            = $_POST['initial']; 
$informedBoolean        = $_POST['informed']; 
$email                  = $_POST['emailAddress']; 



    $consentForm = array( 
          "agreeBoolean"            => $agreeBoolean, 
          "nameInitial"             => $nameInitial,
          "timeOfSigning"           => date("Y-m-d H:i:s"),
          "informed"                => $informedBoolean,
          "email"                   => $email                 
       );
    $collection->insert($consentForm);
    echo "You have Successfully Signed the consent form.....";


?>

After some search, I replaced

$m = new MongoClient();

with

$m = new MongoDB\Driver\Manager("mongodb://localhost:27017");

Then the following error message occured: Notice: Undefined property: MongoDB\\Driver\\Manager::$MergedParticipant in C:\\wamp64\\www\\loginNew.php on line ...

Can you please help,

Many thanks

To use MongoDB with PHP, you need to use MongoDB PHP driver. Download the driver from the url Download PHP Driver. Make sure to download the latest release of it. Now unzip the archive and put php_mongo.dll in your PHP extension directory ("ext" by default) and add the following line to your php.ini file −

extension = php_mongo.dll

tutorialspoint.com/mongodb/mongodb_php.html please refer this link for connect mongodb with php –

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM