本文整理汇总了PHP中MongoClient::connect方法的典型用法代码示例。如果您正苦于以下问题:PHP MongoClient::connect方法的具体用法?PHP MongoClient::connect怎么用?PHP MongoClient::connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoClient
的用法示例。
在下文中一共展示了MongoClient::connect方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: connect
public function connect()
{
if ($this->_connection) {
return;
}
$host = $this->_config['connection']['server'];
if (isset($this->_config['connection']['username']) && isset($this->_config['connection']['password'])) {
$host = $this->_config['connection']['username'] . ':' . $this->_config['connection']['password'] . '@' . $host;
}
if (strpos($host, 'mongodb://') !== 0) {
$host = 'mongodb://' . $host;
}
if (!isset($options)) {
$options = array();
}
$options['connect'] = FALSE;
$this->_connection = new MongoClient($host, $options);
try {
$this->_connection->connect();
} catch (MongoConnectionException $e) {
throw new Exception('Unable to connect to Mongo server at :hostnames', array(':hostnames' => $e->getMessage()));
}
if (!isset($this->_config['connection']['db'])) {
throw new Exception('No database specified in MangoDB Config');
}
$this->_db = $this->_connection->selectDB($this->_config['connection']['db']);
return $this->_connected = TRUE;
}
示例2: connect
/**
* Connects to our database
*/
public function connect()
{
// We don't need to throw useless exceptions here, the MongoDB PHP Driver has its own checks and error reporting
// Yii will easily and effortlessly display the errors from the PHP driver, we should only catch its exceptions if
// we wanna add our own custom messages on top which we don't, the errors are quite self explanatory
if (version_compare(phpversion('mongo'), '1.3.0', '<')) {
$this->_mongo = new Mongo($this->server, $this->options);
$this->_mongo->connect();
if ($this->setSlaveOkay) {
$this->_mongo->setSlaveOkay($this->setSlaveOkay);
}
} else {
$this->_mongo = new MongoClient($this->server, $this->options);
if (is_array($this->RP)) {
$const = $this->RP[0];
$opts = $this->RP[1];
if (!empty($opts)) {
// I do this due to a bug that exists in some PHP driver versions
$this->_mongo->setReadPreference(constant('MongoClient::' . $const), $opts);
} else {
$this->_mongo->setReadPreference(constant('MongoClient::' . $const));
}
}
}
}
示例3: connect
public function connect($servers, $options = null)
{
if (empty($servers)) {
$this->error('not enough information to connect to mongodb.');
}
if (empty($options)) {
$options = array('connect' => true);
}
// checking dependency
if (!class_exists('MongoClient')) {
$this->error('no mongo client class. remember to sudo pecl install mongo.');
}
try {
$connection = new MongoClient($servers, $options);
$ok = $connection->connect();
if (!$ok) {
$this->error('could not connect to mongo.');
}
self::$___connection = $connection;
} catch (Exception $e) {
$this->error('couldn\'t connect to the mongo:' . $e->getMessage());
}
$this->log('connected to ' . $servers . '.');
return self::$___connection;
}
示例4: get_page
function get_page($pageid)
{
global $config;
$connection = new MongoClient($config["database"]["connectionstring"]);
$connection->connect();
$blog = $connection->selectDB($config["database"]["dbname"]);
$pages = $blog->pages;
$page = $pages->findOne(array("_id" => $pageid));
$connection->close();
if (!is_null($page)) {
$page["content"] = Michelf\MarkdownExtra::defaultTransform($page["content"]);
}
return $page;
}
示例5: getDb
public function getDb()
{
if (null === $this->_db) {
$options = $this->getOptions();
// caching ini file data
if (isset($options['host']) && isset($options['dbname'])) {
$url = $options['host'];
if (isset($options['port']) && $options['port']) {
$url .= ":" . $options['port'];
}
if (isset($options['username']) && $options['username']) {
$user = $options['username'] . ':';
if (isset($options['password']) && $options['username']) {
$user .= $options['password'];
}
$url = $user . "@" . $url;
}
$url = 'mongodb://' . $url;
$params = array("connect" => true);
if (isset($options['options']) && !empty($options['options'])) {
$params = $options['options'] + $params;
}
try {
$connection = new MongoClient($url, array('connect' => false) + $params);
$connection->connect();
$db = $connection->{$options}['dbname'];
$this->setDb($db);
\App::alarm()->restoreDBAlarmLog($options['dbname']);
} catch (MongoConnectionException $e) {
\App::log('Error connecting to MongoDB server: ' . $options['host']);
\App::alarm()->errorDBAlarmLog($url);
if ($connection) {
$connection->close(true);
}
throw $e;
} catch (\Exception $e) {
\App::log('Error: ' . $e->getMessage());
\App::alarm()->errorDBAlarmLog($url);
if ($connection) {
$connection->close(true);
}
throw $e;
}
}
}
return $this->_db;
}
示例6: getConnection
public function getConnection()
{
try {
// get mongo db connection
$db = new MongoClient("mongodb://{$this->getHost()}:{$this->getPort()}", array('username' => $this->getUsername(), 'password' => $this->getPassword()));
} catch (MongoConnectionException $e) {
// echo '<pre>';
print_r($e);
// die;
}
// get db
$dbName = $this->getDbname();
$dataBase = $db->{$dbName};
try {
// test mongo connection
$db->connect();
} catch (Zend_Exception $e) {
var_dump($e);
// TODO: uraditi nesto sa exceptionom
}
return $dataBase;
}
示例7: connect
/**
* Database connection
*
* [!!] This will automatically be called by any \MongoDB methods that are proxied via [\Gleez\Mango\Client::__call]
*
* [!!] This **may be** called automatically by [\Gleez\Mango\Client::__construct].
*
* @return boolean Connection status
*
* @throws \Gleez\Mango\Exception
*
* @uses \Arr::path
* @uses \Profiler::start
* @uses \Profiler::stop
*/
public function connect()
{
if ($this->isConnected()) {
return true;
}
try {
if ($this->profiling) {
// Start a new benchmark
$this->benchmark = Profiler::start(__CLASS__ . "::{$this->name}", 'connect()');
}
// Connecting to the server
$this->connected = $this->connection->connect();
if ($this->benchmark) {
// Stop the benchmark
Profiler::stop($this->benchmark);
// Clean benchmark token
$this->benchmark = null;
}
} catch (\Exception $e) {
throw new Exception('Unable to connect to MongoDB server. MongoDB said :message', array(':message' => $e->getMessage()));
}
$this->db = $this->isConnected() ? $this->connection->selectDB(Arr::path($this->config, 'connection.options.db')) : null;
return $this->isConnected();
}
示例8: testMongoConnection
private function testMongoConnection(\MongoClient $mongo)
{
try {
$mongo->connect();
} catch (\MongoConnectionException $e) {
return false;
}
return true;
}
示例9: GetRank
<!DOCTYPE html>
<?php
date_default_timezone_set("America/Sao_Paulo");
$m = new MongoClient('mongodb://localhost');
$m->connect();
$db = $m->Teste;
$collection = $db->IUser;
$user = $collection->findOne(array('Name' => $_GET['name']));
$allBadgesCollection = $db->IBadge;
$badge = $allBadgesCollection->find();
$badge = $badge->sort(array("ExtensionName"));
function GetRank($cod)
{
$path = 'res/Badges/Main/';
$rank = '';
switch ($cod) {
case 1:
$rank = 'diamond.png';
break;
case 2:
$rank = 'corundum.png';
break;
case 3:
$rank = 'topaz.png';
break;
default:
$rank = 'quartz.png';
}
return $path . $rank;
}
示例10: setDb
/**
* @param MongoClient $mongoClient
*/
public function setDb(\MongoClient $mongoClient)
{
$this->db = $mongoClient;
$this->db->connect();
}
示例11: isConnected
/**
* @return bool
*/
public function isConnected()
{
return $this->mongo->connect();
}
示例12: connect
/**
* Force the connection to be established.
* This will automatically be called by any MongoDB methods that are proxied via __call
*
* @return boolean
* @throws MongoException
*/
public function connect()
{
if (!$this->_connected) {
if ($this->profiling) {
$_bm = $this->profiler_start("Mongo_Database::{$this->_name}", "connect()");
}
$this->_connected = $this->_connection->connect();
if (isset($_bm)) {
$this->profiler_stop($_bm);
}
$this->_db = $this->_connection->selectDB("{$this->_db}");
}
return $this->_connected;
}
示例13: function
<?php
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Slim\Http\Request;
use Slim\Http\Response;
// Routes
$app->group('/api/quotes', function () {
// Setup DB connection
$mongo = new MongoClient('mongodb://localhost:27017');
$mongo->connect();
$db = $mongo->test;
$quotes = $db->selectCollection('quotes');
$this->get('', function (Request $request, Response $response, array $args) use($quotes) {
$this->logger->info("Fetching 10 records…\n");
$results = [];
foreach ($quotes->find([], ['_id' => 0])->sort(['index' => -1])->limit(10) as $quote) {
$results[] = $quote;
}
return $response->getBody()->write(json_encode($results, JSON_PRETTY_PRINT));
});
$this->post('', function (Request $request, Response $response, array $args) use($quotes) {
$quote = json_decode($request->getBody()->getContents(), JSON_OBJECT_AS_ARRAY);
if (!isset($quote['content'])) {
return $response->withStatus(400, 'Post syntax incorrect.');
}
$quote['index'] = $quotes->count();
$quote['index']++;
try {
$quotes->insert($quote);
return $response->withStatus(201, "Created")->getBody()->write(json_encode($quotes->count(), JSON_PRETTY_PRINT));