【问题标题】:Mongodb connection not workingMongodb连接不工作
【发布时间】:2014-01-13 13:50:49
【问题描述】:

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

我的代码如下所示:

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

公共函数getCollection($name) {return $this->database->selectCollection($name);}

【问题讨论】:

  • 请格式化您的代码以使其更具可读性。另外——不清楚你的问题是什么? MongoDB 正在运行吗?如果他们以前没有使用过服务/守护进程/进程,很多人会忘记实际启动它。
  • 你能给我正确的答案吗?这样我就可以写了!
  • 您没有解决格式问题,或澄清您的问题是什么。 MongoDB 正在运行吗?
  • 我正在尝试在一个页面上建立 mongodb 连接来连接我的所有页面,基本上我想要一个 mongodb 页面!
  • 感叹号有必要吗? MongoDb 正在运行吗?我问过好几次了。

标签: php mongodb


【解决方案1】:

你在错误的行结束了你的数据库类,试试这个

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

【讨论】:

    猜你喜欢
    • 2017-06-17
    • 2021-11-23
    • 2021-03-20
    • 1970-01-01
    • 2019-10-17
    • 2015-08-22
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多