I need a way to display a version of Mongo database in the php website.
For example, I want to show something like "powered by MongoDB version 2.2.6"
I cannot find a way to find out the version of database that php mongoClient is connected to.
I'm sure that is a way to do this. Does anyone know how?
$mongo = new \Mongo();
$admin = $mongo->admin;
$infos= $admin->command(array('buildinfo'=>true));
$version = $infos['version'];
die($version);
(You need to be admin to show it)
UPDATE : Without authentication, you can check it by using a MongoDB instance.
$c = new \MongoClient();
$db = 'yourdbname';
$mongo = new \MongoDB($c, $db);
$mongodb_info = $mongo->command(array('serverStatus'=>true));
$mongodb_version = $mongodb_info['version'];
die($mongodb_version);
Check out the serverStatus command:
http://docs.mongodb.org/manual/reference/command/serverStatus/
If you're using the PHP driver, check out:
http://php.net/manual/en/mongodb.command.php
to see how you can run that command.
I am not sure, maybe this:
<?php
$v = `mongo --version`;
print_r($v);
?>
$server_status = db->command(array('serverStatus' => TRUE));
will give you lots of information including version
(you need to be admin to run it)
Related
I have problem with hscan command in php.
When i run command in redis-cli everything works well.
hscan some:key 0 match *word*
But in php i've got empty result.
<?php
$it = null;
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$results = $redis->hscan('products:search2', $it, '*word*');
var_dump($results);
I use phpredis php extension.
Why is that?
Ok, i've got this. I just forget to select database to 1,
because my data is on database 1.
Redis as a default look in database 0.
The answer is:
<?php
$redis->select(1);
I am porting a site running PHP with an MS Access DB on a windows machine to a Mac with an SQLite DB.
the original PHP script uses the following code to connect to the database:
$db = 'S:\~myhome\mydata.mdb';
$conn = new COM('ADODB.Connection');
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$db");
What would be the SQLite equivalent?
Edit:
I tried
$db = 'sqlite:'.__DIR__.'/mydata.sqlite';
$conn = new PDO($db) or die("cannot open the database");
but it didn't work
Like Python, PHP has a built in SQLite library. Current versions support SQLite3. First, uncomment out the php_sqlite extension in the .ini file. Then, simply, call a new object:
<php
$conn = new SQLite3($db);
$results = $conn->query('SELECT bar FROM foo');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>
Of course as suggested you can use PDO or mysqli database connections.
Since you have to work on it anyway, I suggest to use PDO. This is a standard PHP library with drivers for several database types.
For a quick start see http://www.phptherightway.com/#databases, there's a short example about SQLite as well.
after much searching I found the answer:
include '/usr/share/php/adodb/adodb.inc.php';
$path = urlencode(__DIR__.'/mydata');
$dsn = "sqlite://$path/?persist"; # persist is optional
$conn = ADONewConnection($dsn);
I recently discovered cloudcontrol and wanted to write a little mongoDB app to store some images. I enabled the free addon mongolab.free and followed the documentation to set up a connection.
$credfile = file_get_contents($_ENV['CRED_FILE'], false);
$credentials = json_decode($credfile, true);
$uri = $credentials["MONGOLAB"]["MONGOLAB_URI"];
$m = new Mongo($uri);
$db = $m->selectDB('test');
The problem is that the following line doesn't work because I "Need to login"
$db = $m->selectDB('test');
If I try to list all available DBs via listDBs() I get the same error "Need to login"
Even when connection via the terminal, the connection establishes but with the same "need to login" warning.
I've been trying to fix this problem since three days now but I cannot find the solution :(
Hope someone can help me
The name of your database is defined in MONGOLAB_URI as well (it has the form cloudcontrol_<deployment_id>). So you don't have to explicitely define another database name (and can't).
You can extract the database name from the uri with
preg_match("#/([^/\?]+)(\?|$)#", $uri, $match);
database = $match[1];
Please help. I've also given the code below. Tried so many things. Isn't working.
selected a spatialite database:
$db = new SQLite3('db2.sqlite');
tried these load extensions:
// $db->loadExtension('C:\PHP\ext\libspatialite-2.dll');
// $db->loadExtension('libspatialite-4.dll');
// $db->exec("SELECT InitSpatialMetadata()");
also tried this function in case if loading extension is disabled:
// $db->exec("sqlite3_enable_load_extension(1)");
// $tvalue=$db->loadExtension('C:\\PHP\\sqliteext\\libspatialite-4.dll');
tried load extension here:
$db->exec("SELECT load_extension('C:\\PHP\\sqliteext\\libspatialite-4.dll')");
phpinfo();
$result = $db->query("SELECT load_extension('libspatialite-4.dll')");
var_dump($result->fetchArray());
tried this query:
$result = $db->query('select astext(geometry) from roads');
var_dump($result->fetchArray());
$rs = $db->query('SELECT spatialite_version()');
One of the most common problems is using a version of libspatialite that is an "amalgamation" build, which includes sqlite. That is problematic (especially on 64 bit windows) if the version of sqlite that is included doesn't match the version that you're using to load libspatialite. Two versions with different ABI = crash.
I don't have a nice solution, but there are things you can do to try to avoid this:
1. don't use an amalgamation build
2. make sure that the two versions of sqlite are the same.
Here is the php code that I'm working with in my local machine:
$m = new Mongo();
$db=$m->selectDB("def");
//then all in my code i use $db to select insert ... (as defined in php doc)
Now I want to connect my application to a remote server (hosted by mongood.com)
How can I do this?
You can use mongoOd without the REST API
But remember, it's a replica Set cluster so You need to configure your PHP for a ReplicaSet configuration...
I use mongoOd within ruby & mongoid (not the REST API)
Here a php example
<?php
// connecting to mongood.com cluster
$m = new Mongo("mongodb://94.23.54.103:27017,188.165.219.99:27017,94.23.220.151:27017", array("replicaSet" => "cluster"));
var_dump($m);
$db = $m->selectDB('my_database');
$db->authenticate("my_login", "my_password");
$collection = new MongoCollection($db, 'my_collection');
$cursor = $collection->find();
foreach ($cursor as $doc) { var_dump($doc); }
?>
Enjoy :)
A mongoOd Team member
The constructor for the mongo object takes as its arguments connection parameters.
http://www.php.net/manual/en/mongo.construct.php
$m = new Mongo('mongodb://[username:password]#host:port')
You'll have to ask them what the connection URI is, and then use:
$m = new Mongo("mongodb://username:password#hostname");
However, I am not sure if that option is available to you. Their website says you can access data via a REST API.
At any rate, you should ask them for help. There's a button on the left that reads "aide," if you click on it you'll get a form where you can fill in your email and your question.
Reference: Mongo - Connecting