简体   繁体   中英

MongoDB Dynamic DB Selection / Creation

In Mongo DB you can do the following :

$m = new Mongo();
// select a database
$db=$m->databaseName;    // this creates/returns the db "databaseName"

How can I make a function such that dataBasename is passed as a dynamic variable? The below function currently fails.

load_db("databaseName");    

function load_db($dbname)
{
$m = new Mongo();
$db=$m->$dbname;   // I have also tried {$dbname}, with no success.

return $db;
}

You can use $db = $m->selectDb( $variable ); : http://php.net/manual/en/mongo.selectdb.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