Class 'MongoDB\Client' not found, mongodb extension installed - php

I tried to create new mongo connection executing the following code
$m = new MongoDB\Client();
and i got this error:
Fatal error: Class 'MongoDB\Client' not found
i think i have properly installed MongoDB extension
(Copied php_mongodb.dll to ext folder and updated php.ini with extension=php_mongodb.dll).
The following code confirms it is loaded:
echo extension_loaded("mongodb") ? "loaded\n" : "not loaded\n";
I still receive the same error.
Here is
phpinfo()
I appreciate all your help. Thank you!

If you are using latest MongoDB extension of PHP, MongoDB\Driver\Manager is the main entry point to the extension.
Here is the sample code to retrieve data using latest extension.
Let's say you have testColl collection in testDb. The you can retrieve data using MongoDB\Driver\Query class of the extension.
// Manager Class
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
// Query Class
$query = new MongoDB\Driver\Query(array('age' => 30));
// Output of the executeQuery will be object of MongoDB\Driver\Cursor class
$cursor = $manager->executeQuery('testDb.testColl', $query);
// Convert cursor to Array and print result
print_r($cursor->toArray());
Output:
Array
(
[0] => stdClass Object
(
[_id] => MongoDB\BSON\ObjectID Object
(
[oid] => 5848f1394cea9483b430d5d2
)
[name] => XXXX
[age] => 30
)
)

I'm using PHP 7.1.9 and I had this issue. Solved it by removing and reinstalling mongodb/mongodb
composer remove mongodb/mongodb
composer require mongodb/mongodb
Also, If you are using Dreamweaver, don't for get to put the vendor folder in the server copy.
After installing, I can now use MongoDB\Client.
mongodb API Version 1.3, Mongodb Extension 1.4

same happened with me, check the version of php install on your server.
You have to use php version 5.6 .Check the apche error log to get more precise error detail.

Just simple way to install
sudo apt-get install php-mongodb
after install mongo

Different for Apache and Nginx it appears.
Though this post is old but I help this migh help someone. I recently ran into same problem. With me it was s different php.ini.
I kept placing the mongoextension into /cli/php.ini.
Thoough when I ran <?php phpinfo() ?> I found out that in my case the loaded configuration is in /fpm/php.ini. This was because I was unsing Nginx with fpm.

90% of the time it is your Mongodb extension configuration. kindly check in php.ini

\MongoDB\Driver\Manager(); is now "low level" extension (php_mongodb), \MongoDB\Client was rewritten to "PHP class" and you need extra install https://docs.mongodb.com/drivers/php/ to use old style nice new \MongoDB\Client('mongodb://127.0.0.1');

Related

How to install locally an extension in SQLite on a Mac?

I've no real knowledge about installing an extension. I'm stuck because I cannot use SQRT in SQLite.
I'm using Laravel on a local machine and I'm working on my PHPunit Tests. In one Test I'm using a query with some heavy math functions.
I get the error:
Caused by
PDOException: SQLSTATE[HY000]: General error: 1 no such function: SQRT
I found out that SQLite has an extension called: extension-functions.c
I downloaded it, but I'm not sure if I did everything correctly (obviously not because it does not work):
I downloaded it and used this in my terminal
gcc -fno-common -dynamiclib extension-functions.c -o libsqlitefunctions.dylib
It compiled this data: libsqlitefunctions.dylib
With php --ini if found that my php.ini is located in /usr/local/etc/php/7.1/php.ini
Now I looked up for sqlite3 and found this commented ;sqlite3.extension_dir =
I changed it to
sqlite3.extension_dir =/usr/local/etc/php/7.1/extension/libsqlitefunctions.dylib
and created the extension folder and put the dylib file inside
I restarted my apache with sudo apachectl restart
And tried my Query and still, it fails. What did I do wrong?
For future readers
To use it in Laravel you call it like this:
$pdo = DB::connection()->getPdo();
$pdo->sqliteCreateFunction('SQRT', 'sqrt', 1);
$pdo->sqliteCreateFunction('ASIN', 'asin', 1);
$pdo->sqliteCreateFunction('SIN', 'sin', 1);
$pdo->sqliteCreateFunction('COS', 'cos', 1);
$pdo->sqliteCreateFunction('POWER', 'pow', 2);
$pdo->sqliteCreateFunction('pi', 'pi', 0);
It seems that you want to use this with PDO. The problem is that loading extensions only works with the SQLite3 api directly, using the SQLite3::loadExtension method.
The PDO driver does not allow the loading of extensions (see this wiki entry regarding LoadableExtensions under "Security Considerations").
So you could either swith to the SQLite3 api directly or you could use "user defined functions" with PDO. For example if you need the SQRT function:
$db = new PDO('sqlite:testdb.sqlite', null, null, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
$db->sqliteCreateFunction('SQRT', 'sqrt', 1);
$stmt = $db->query("SELECT sqrt(25)");
foreach ($stmt as $row) {
echo $row[0], PHP_EOL; // output: 5.0
}
Most of the math-functions your extension provides map 1:1 to the PHP function names (like sqrt), so it should be easy to create the functions you need.
It seems you are pointing the sqlite3.extension_dir property at a file instead of a directory. According to the manual for sqlite3, the extension_dir property is:
[The] Path to the directory where the loadable extensions for SQLite reside.
I think it may work if you change
sqlite3.extension_dir = /usr/local/etc/php/7.1/extension/libsqlitefunctions.dylib
to
sqlite3.extension_dir = /usr/local/etc/php/7.1/extension
However, I would create a separate directory for the SQLite3 extensions so they are not mixed with the PHP extensions; I say this since it seems like you are currently using the PHP extension directory for the sqlite3.extension_dir property. (If you aren't, then please disregard this note.) I assume that SQLite3 just scans the configured directory for loadable modules and loads them using something like dlopen. This might conflict if it tries to load a PHP extension. (Who knows, it may gracefully handle this.)

Error GRPC Spanner Google Cloud With PHP

I'm using PHP to try using the Google Cloud Spanner. I already did the gCloud settings and everything, and that's right. Now I need to make the connection via PHP to do a CRUD with the database that is in Spanner, but the code below always returns the error:
PHP Fatal error: Undefined constant 'Grpc\STATUS_UNKNOWN' in
/xxx/xxxx/www/vendor/google/cloud-spanner/Connection/Grpc.php on line
129
The code I have is:
<?php
require 'vendor/autoload.php';
use Google\Cloud\Spanner\SpannerClient;
/* Error start here */
$spanner = new SpannerClient([
'projectId' => 'my-project-id'
]);
$db = $spanner->connect('instance', 'database');
$userQuery = $db->execute('SELECT * FROM usuario WHERE login = #login', [
'parameters' => [
'login' => 'devteam'
]
]);
$user = $userQuery->rows()->current();
echo 'Hello ' . $user['login'];
The requirements I use in the composer are:
"require": {
"google/cloud": "^0.32.1",
"google/cloud-spanner": "^0.2.2"
}
I noticed that if I enter through the browser, the error presented above continues to appear. If I run the command php teste.php on the terminal, it runs the script correctly, ie, the terminal works and the browser does not.
Google Cloud PHP's spanner client is gRPC only. This means to use it you will need to install the gRPC PHP extension:
pecl install grpc
Once you have done that, add google/proto-client-php and google/gax to your composer.json and run composer update. After this is done, the error will be resolved.
For those wanting more detailed instructions, see this page for installing and enabling gRPC for PHP!
Since you mentioned that it works on CLI but not on browser, I can say that you need to enable the grpc extension on your php web server config.
E.g. Add
extension=grpc.so to your /etc/php/5.6/apache2/php.ini

PHP and MongoDB

I am trying to connect to MongoDB via PHP.
mongod --version
db version v3.2.8
Then,
php -i | grep mongo
/etc/php/7.0/cli/conf.d/20-mongodb.ini,
mongodb
mongodb support => enabled
mongodb version => 1.1.8
mongodb stability => stable
libmongoc version => 1.3.5
mongodb.debug => no value => no value
I tried:
sudo pecl install mongodb
which returns
pecl/mongodb is already installed and is the same as the released version 1.1.8
install failed
which (the install failed bit) gets redressed if I try:
sudo pecl uninstall mongodb
and
sudo pecl install mongodb
I have this php file:
<?php
echo "I am here";
$connection = new Mongo('localhost');
$db = $connection->mydb;
$list = $db->listCollections();
foreach ($list as $collection) {
echo "$collection </br>";
}
echo "I am never here";
?>
I cannot see the second echo.
I would appreciate any thoughts.
Thank you.
First, check #sanjay comment and then apply this connection:
$db = new MongoClient('mongodb://localhost', [
'username' => 'root',
'password' => '',
'db' => 'YOUR DB'
]);
Hope it will work for you.
I managed to have it working. Thanks to this post:
After upgrading PHP to version 7, why can't I use the mongodb driver?
and this post:
Using the PHP Library for MongoDB (PHPLIB)
Sorry as it seems pretty odd (to me!) but indeed: MongoClient() has become (verbatim! with the slash!) MongoDB\Client().
Hence, the file I had in my original post becomes:
<?php
require 'vendor/autoload.php';
echo "I am here";
$manager = new MongoDB\Client();
$database = $manager->mydb;
foreach ($database->listCollections() as $databaseInfo) {
var_dump($databaseInfo);
}
echo "I managed to arrive here";
?>
and I can see the contents of mydb as well as the 2 echos. The vendor/autoload.php was generated by
sudo composer require "mongodb/mongodb=^1.0.0"
as I ran it from within the directory of the php file above. Probably not the best idea as I, now, have to resolve how to make the autoload.php (and all it carries with) globally available. But, nonetheless, at least, I managed to arrive somewhere.
I am using: PHP 7.0.8-0ubuntu0.16.04.2 (cli) ( NTS )
under: Ubuntu 16.04.1 LTS

PECL_HTTP not recognised php ubuntu

I am trying to use the HttpRequest class which should be in the PECL_HTTP extension of php.
I have php5 and used the following to install pecl_http.
sudo pecl install pecl_http
pecl/pecl_http is already installed and is the same as the released version 2.0.1
install failed
After that i enter into my php.ini:
[PHP]
extension=http.so
[...]
Then i restart my apache2 server, and try using the HttpRequest class, but it gives me the following error:
PHP Fatal error: Class 'HttpRequest' not found
What are possible error's i might have missed?
UPDATE: (Changed the title)
The Extension is not shown in phpinfo(), i set
extension=http.so
And checked if the http.so file is in my extension_dir. I really don't know how to make php recognise the extension.
UPDATE 2:
I managed to install extension, but the class still does not exist.
For others, i had to reference the other extensions pecl_http needs. (For me: propro.so, raphfr.so)
UPDATE 3:
I did not manage to make the Class visible, the answers below show some approaches with other classes.
I solved this issue by using CURL.
You have installed the new version of the extension which uses a completely different API. I still don't know how it works, but I will update my answer one I know. The Documentation of the new version is to be found at http://devel-m6w6.rhcloud.com/mdref/http.
To Install the old version, first uninstall the new verion and then execute
pecl install http://pecl.php.net/get/pecl_http-1.7.6.tgz
UPDATE: here are two examples from the documentation, both should work well:
one request (can be found at http://devel-m6w6.rhcloud.com/mdref/http/Client/enqueue):
<?php
(new http\Client)->enqueue(new http\Client\Request("GET", "http://php.net"),
function(http\Client\Response $res) {
printf("%s returned %d\n", $res->getTransferInfo("effective_url"), $res->getResponseCode());
return true; // dequeue
})->send();
multiple requests (can be found at http://devel-m6w6.rhcloud.com/mdref/http/Client/once):
<?php
$client = new http\Client;
$client->enqueue(new http\Client\Request("HEAD", "http://php.net"));
$client->enqueue(new http\Client\Request("HEAD", "http://pecl.php.net"));
$client->enqueue(new http\Client\Request("HEAD", "http://pear.php.net"));
printf("Transfers ongoing");
while ($client->once()) {
// do something else here while the network transfers are busy
printf(".");
// and then call http\Client::wait() to wait for new input
$client->wait();
}
printf("\n");
while ($res = $client->getResponse()) {
printf("%s returned %d\n", $res->getTransferInfo("effective_url"),
$res->getResponseCode());
}
In both examples you can use $res->getBody() to return the body of the response.
I couldn't test these examples but I have heard of other people that they work.
I had the same problem and solved it by arrange the order in wich extensions are loaded in php.ini
All my other extensions was loaded using their own .ini files in /etc/php5/mods_available.
In my apache2 error.log I noticed that json_decode was needed by http.so to load.
I created a file for http (http.ini) and a symlink in /etc/php5/apache/conf.d with a higher prefix than json.
my http.ini
; configuration for php http module
; priority=50
extension=raphf.so
extension=propro.so
extension=http.so
use the new pecl_http.2.0.6 like this
new http\Message();
or extend in your own class as extends http\Message

Install ingres extension for php on my WAMP server

I've a development computer, which runs under windows.
For a project, I've to make a php website which has to connect to an Ingres database server.
So I installed wamp, I installed ingres(server and client, on my local machine).
I added the library that I found on their site(php_ingres.dll) in the C:\wamp\bin\php\php5.3.5\ext folder, and I added a line "extension=php_ingres.dll" in the configuration file.
I shutdown wamp and restarted it, and I restarted the server, I see now a check mark in the wamp menu, indicating that php_ingres is now activated. But when I go to the welcome page of the server, I don't see this extension as loaded. If I go on the php info page, I don't see any Ingres entry in the Configure Command.
I just can't found any post/tutorial/... which indicating how to do this operation, so any help would be appreciated!
Thank you!
Edit: I made a small test to see if I can connect to an Ingres database:
<?php
$link = ingres_connect("localhost", "demodbtest", "demodbtest") or die("Connexion impossible");
echo "Connexion réussie";
$result = ingres_query($link,"select * from airline");
while ($row = ingres_fetch_array($result)) {
echo $row["al_iatacode"]; // utilisation du tableau associatif
echo $row["al_name"];
echo $row["al_ccode"]; // utilisation du tableau à indices numériques
echo "</br>";
}
ingres_close($link);
?>
And I get this error:
( ! ) Fatal error: Call to undefined function ingres_connect() in
C:\wamp\www\tests\index.php on line 2
Some information on my installation:
I've a windows 7 pro 32bits
Wampserver 2.1 ( http://sourceforge.net/projects/wampserver/files/WampServer%202/WampServer%202.1/WampServer2.1e-x32.exe/download )
Apache 2.2.17
PHP 5.3.5
Ingres 10.1.0 Community edition( downloaded here: http://esd.ingres.com/product/Community_Projects/Ingres_Database/Windows_32-Bit/Ingres_10.1_Build_121/ingres-10.1.0-121-gpl-win-x86-NoDoc.zip/http )
PHP drivers downloaded here: http://esd.ingres.com/product/drivers/PHP/Windows_32-Bit/PHP_Driver
To practically test if the extension was loaded you can as well call one of it's functions. If the extension was loaded, you should not get a fatal error for a missing function. That's perhaps one of the quickest checks.
Another check is to make use of extension_loaded *PHP Manual** which will give you a list of all loaded extensions. See the PHP Manual link above for more info.
The configure line
The configure line will not show the ingres extension because it has not been compiled in. That's perfectly alright because you load it as an extension (.dll) so it's not part of php.exe. This is why you don't see it in the configure line.
Locating ingres on the phpinfo() page.
On the phpinfo()-page use the search function inside your browser (often CTRL+F) and try to locate the word ingres. You should locate a section that displays the extensions default settings if it has been loaded.
The following is an example screenshot for the xdebug extension. This might look similar for ingres:
Image from: Launching xdebug in Eclipse stuck at 57% - How to trouble-shoot?
Double check your extension_dir setting as well as the actual php.ini file being used. Calling php.exe -i from the command line might not give the same output if executing phpinfo() in a script via Apache (or IIS). In fact http://www.wampserver.com/en/faq.php says there are 3 potential php.ini scripts.
The problem is that I wasn't having the ingres client installed locally, so it appears that this lib cannot works without it

Categories