我有以下代码连接到MongoDB中:如何从另一个PHP类连接到MongoDB?
try {
$m = new Mongo('mongodb://'.$MONGO['servers'][$i]['mongo_host'].':'.$MONGO['servers'][$i]['mongo_port']);
} catch (MongoConnectionException $e) {
die('Failed to connect to MongoDB '.$e->getMessage());
}
// select a database
$db = $m->selectDB($MONGO["servers"][$i]["mongo_db"]);
然后我创建了一个PHP类,我想在蒙戈检索/更新数据。我不知道如何访问以前创建的Mongo连接。
class Shop {
var $id;
public function __construct($id) {
$this->id = $id;
$this->info = $this->returnShopInfo($id);
$this->is_live = $this->info['is_live'];
}
//returns shop information from the database
public function returnShopInfo() {
$where = array('_id' => $this->id);
return $db->shops->findOne($where);
}
}
和代码是一样的东西:
$shop = new Shop($id);
print_r ($shop->info());
嗨德里克,如果“如果”条件满足什么都不会返回任何? – 2013-11-01 10:21:37
好点,我修正了这个例子 – Derick 2013-11-25 11:10:30