When I'm trying to install new dll for PHP 7 version, i am confused with the two links,
https://pecl.php.net/package/mongodb
https://pecl.php.net/package/mongo
Please can anyone explain the difference between these two links?
Mongo (whithoud the 'db') is the old driver. MongoDB is the new driver. You can read about the new driver at https://derickrethans.nl/new-drivers.html
The new driver is to be used with the library https://github.com/mongodb/mongo-php-library
Related
I'm trying to connect PHP 7 with mongoDB, I installed the "new" MongoDB driver using pecl by following this page instructions. I can see MongoDB version 1.1.8 from phpInfo() output, but I can't figure out how to initiate a connection from PHP code :p . the following code includes my attempts to connect (tried to connect even using old fashion way)
// new fashion way
$connection = new MongoDB\Driver\Client();
// or by using old fashion way
$conn = new MongoClient();
// random try :p
$randConn = new MongoDB\Client();
and in both cases, I'm getting not defined class exception.
please let me know what I'm missing and where is my mistake, please provide and example to be easier to follow if possible ;) .
PS: used operating system is ubuntu 14.04 LTS.
thanks in advance.
The page that you are referring to is the low-level PHP driver for MongoDB. The API is the same as the HHVM driver for MongoDB. The documentation for both of them is the same, and can be found at http://docs.php.net/manual/en/set.mongodb.php
The driver is written to be a bare bone layer to talk to MongoDB, and therefore misses many convenience features. Instead, these convenience methods have been split out into a layer written in PHP, the MongoDB Library. Using this library should be your preferred way of interacting with MongoDB.
The library needs to be installed with Composer, a package manager for PHP. See also Get Composer: Installation on Linux/OSX
For example:
composer require "mongodb/mongodb=^1.0.0"
Once you have it installed, you can try connecting using:
<?php
require 'vendor/autoload.php';
$collection = (new MongoDB\Client("mongodb://127.0.0.1:27017"))->dbname->coll;
?>
See also:
Doc: MongoDB PHP Library
MongoDB PHP Library: Getting Started
PHP MongoDB Driver
I have MongoDB installed in my cPanel/WHM CentOS server.
I have the PHP Drivers installed.
I have Port 27017 opened.
This works $m = new MongoDB\Driver\Manager();
These do not work:
$m = new Mongo();
$m = new MongoClient();
Both result in Fatal error: Class 'Mongo' not found in ... and Fatal error: Class 'MongoClient' not found in ....
Does anyone know why?
Not a duplicate. That SO Q is for a Windows machine. My server is Linux/CentOS
From My Hosting Provider's Sys Admin:
It looks like "mongo" is a seperate php module from "mongodb", and that "mongodb" is the newer one:
root#host [~/support/642192]# pecl search mongo
Retrieving data...0%
.Matched packages, channel pecl.php.net:
Package Stable/(Latest) Local
mongo 1.6.12 (stable) MongoDB database driver (legacy)
mongodb 1.1.2 (stable) 1.1.2 MongoDB driver for PHP
Did you need the legacy module "mongo" instead of "mongodb"? Here is hte pecl page for the package you have:
https://pecl.php.net/package/mongodb
and here is the one for the legacy module:
https://pecl.php.net/package/mongo
Short answer
You cannot use the following classes with the new mongodb driver:
$m = new Mongo();
$m = new MongoClient();
This corresponds to the legacy mongo driver. Instead you should use MongoDB\Client through the MongoDB PHP Library.
Long answer
All right. I lost some hair on this story as well, because the documentation about Mongo and PHP is extremely confusing. The options are then to get mad, bald, or both. But i found courage and finally got it. So this might help you.
1. MongoDB driver: mongo vs mongodb
First of all, you must clarify which MongoDB driver you use: either mongo (legacy) or mongodb (new). Note the smaller case. First source of confusion, the mongo driver is sometimes referred to as MongoDB (legacy) PHP driver.
https://docs.mongodb.com/ecosystem/drivers/php/
Second source confusion, the version numbers are not logical, as mongo driver had 1.5, 1.6 but mongodb starts again from 1.0. So, going forward, but backwards, it's just insane... Imo they should have started from 2.0!
The choice of the driver depends on your PHP version:
PHP5.3: you can only use the mongo legacy driver (1.5, 1.6)
PHP7.0: you can only use the new mongodb driver (1.1+)
PHP5.4, 5.5, 5.6: here you have the choice between the old mongo (1.5, 1.6) or the new mongodb (1.0, 1.1+)
But it's not over. The driver is just a low-level interface (aka PHP extension). Now we come to the programmer's API and it becomes even worse.
2. API: MongoClient vs MongoDB\Driver
Third source of confusion, the low-level drivers and the API classes have overlapping names.
mongo legacy driver -> classes MongoClient, MongoDB (!), ...
mongodb driver -> classes MongoDB\Driver, MongoDB\BSON, ...
But it's not over yet. The old mongo driver could be used directly. The new mongodb driver provides classes (such as MongoDB\Driver) but it is actually a low-level API. You are not supposed to use it directly, you could, but it's not convenient. Instead you should use the MongoDB PHP Library which gives an API similar to the old MongoClient classes...!
3. MongoDB PHP Library (with mongodb) -> MongoDB\Client
https://docs.mongodb.com/php-library/master/
So if you installed mongodb, you should install this MongoDB PHP Library in order to use MongoDB\Client. This class is supposed to be similar to the old MongoClient, but there are some differences such as the sort and projection.
To install this library you are advised to use the tool called Composer which allows to you download these classes into your repo. Then use the autoloader provided with Composer.
And here we come to the 4th source of confusion, MongoDB PHP Library is versioned from 1.0 even though you are using the last mongodb driver in version 1.1 ! It's certainly obvious for those who developed this stuff but very hard to follow for lambda users. Crazy confusing stuff.
There are also many other libraries above the drivers, but i don't know them at all so i won't go more into them (https://docs.mongodb.com/ecosystem/drivers/php-libraries/).
TL;DR
Depending on your PHP version, clarify which MongoDB driver to use: mongo (legacy) or mongodb (new) ?
with mongo you can use MongoClient class directly.
with mongodb you should also install MongoDB PHP Library to use MongoDB\Client class.
Good luck! :)
Looks like you haven't enabled or installed php_mongo extension.
After enabling it restart apache and check phpinfo() to see if its properly enabled.
You have use the correct namespace. As stated in their docs the namespace for the client is MongoDB\Client. Alterantivly you can use a use statement like this use MonogoDB\Client.
I have been searching mssql driver for Laravel framework, but had no luck so far. I know Laravel has sqlsrv support for sql servers, but what I need is mssql connector (ones that have functions starting with 'mssql_').
We cannot use sqlsrv because most of our servers are MSSQL 2000, and sqlsrv doesn't provide support for these servers as far as I know. Also, MS does not provide sqlsrv driver for linux. (We are using freetds driver with manually compiled php 5.4.8 on Fedora 17 64-bit).
My question is
Is there any patch for mssql support in Laravel?
If not, which files should I create/modify to have mssql supported if it is ever possible?
I could use different framework that supports mssql as well, but I really like how Laravel handles everything. I didn't want to give up on Laravel just because it doesn't support mssql.
It seems it does not include that driver... I believe you can setup it at laravel/database/connectors
In PHP how to check if the CORRECT mongodb PHP driver is installed for the corresponding mongodb.
The link below tells how to check if mongodb PHP driver is installed, but does not tell if that driver is correct/compatible for the corresponding mongodb.
http://stackoverflow.com/questions/11134959/check-if-mongodb-php-driver-is-installed
For example, if I have PHP version 5.3.10 and have mongodb 2.2.2, the command
echo extension_loaded("mongo") ? "loaded\n" : "not loaded\n";
will say loaded, however, mongodb is not going to work properly because for mongodb 2.2.2, you need the latest PHP not 5.3.10
I think, you're talking about driver, not PHP itself. PHP has no built-in support to access \Mongo* classes until you compile and load special extension.
Since you're talking about the latest version of MongoDB, I think you couldn't use some parts of its functionality because you had some old driver (say, 1.2.12). When you upgraded PHP, you, probably, updated the driver to the latest (1.3.0) stable version as well. That version of driver was submitted a couple of days ago, and it supports all the latest features MongoDB provides.
Anyway, if you'd like to check which version of driver you have, you can call phpinfo(8) from your PHP and look though the output for the mongo section, where the version of driver is displayed.
Will I be able to connect to this database using PHP's php_mongo.dll driver?
If so, could you please provide some sample code?
MongoDB requires its own driver.
In your case, the PHP driver is located here:
http://php.net/manual/en/book.mongo.php
The instructions for installing it are on that page. There is also some sample code.
I think that PDO is only for relational databases. There's no PDO driver for MongoDB.
There is list of databases which you can use with the PDO driver: http://php.net/manual/en/pdo.drivers.php. MongoDB is not among them.
You can use the PHP PDO OBDC library, if you install MongoDB BI Connector and MongoDB ODBC Driver for BI Connector.
I haven’t tried it, but I’m about to - keep you posted.
https://www.mongodb.com/download-center/bi-connector
https://docs.mongodb.com/bi-connector/master/reference/odbc-driver/