Mongodb连接无法正常工作

时间:2013-12-25 18:51:47

标签: php mongodb

嗨,我遇到了几个问题。请帮帮我!

我的代码如下所示:

session_start(); $GLOBALS["config"] =array( "mongodb"=> array( "host"=> "127.0.0.1","username"=>"root", "password"=>"","db"=>"lr"),"remember"=> array( "cookie_name"=>"shoeib", "cookie_expire"=> 604800 ),
"session"=> array( "session_name"=>"users") );
spl_autoload_register(function($class) { require_once"classes/" . $class.".php"; }); require_once "functions/sanitize.php"; 

这是我只创建数据库的数据库!

class db{



private static $_instance= null;
private $_pdo,
$_query,
$_error= FALSE,
$_result,
$_count= 0;

public function __construct(){ try { $this->$connection = new Mongoclient("mongoclient:host".config::get ("mongodb/host").";dbname=".config::get("mongodb/db"),config::get("mongodb/username"),config::get ("mongodb/password"));$this->database = $this->connection-> selectDB(DBConnection::DBNAME);} catch (MongoConnectionException $e)throw $e; } }public static function getInstance(){ if (!isset(self::$instance)) { self::$instance = new db;} return self::$instance;}

public function getCollection($ name){return $ this-> database-> selectCollection($ name);}

1 个答案:

答案 0 :(得分:0)

你在错误的行上结束了你的db类,试试这个

class db
{

    private static $_instance = null;
    private $_pdo,
        $_query,
        $_error = FALSE,
        $_result,
        $_count = 0;

    public function __construct()
    {
        try {
            $this->$connection = new Mongoclient("mongoclient:host" . config::get("mongodb/host") . ";dbname=" . config::get("mongodb/db"), config::get("mongodb/username"), config::get("mongodb/password"));
            $this->database = $this->connection->selectDB(DBConnection::DBNAME);
        } catch (MongoConnectionException $e)throw $e;
    }

    public static function getInstance()
    {
        if (!isset(self::$instance)) {
            self::$instance = new db;
        }
        return self::$instance;
    }
    public function getCollection($name)
    {
        return $this->database->selectCollection($name);
    }
}

您的MongoClient使用情况似乎不正确,但我可能错了PHP.net MongoClient

public MongoClient::__construct() ([ string $server = "mongodb://localhost:27017" [, array $options = array("connect" => TRUE) ]] )

另请查看this tutorial