Class 'MongoClient' not found - php

I'm trying to make this code run:
<?php
$m = new MongoClient("mongodb://54.72.237.242");
$db = $m->tilbud;
?>
Everytime I got the same error:
Fatal error: Class 'MongoClient' not found in C:\xampp\htdocs\conexion.php
I've been reading about this problem the whole day but anything solves my issue (I guess it's something I'm doing wrong).
I downloaded the php_mongo.dll, I copied it in xampp/php/ext and I added extension=php_mongo.dll in the php.ini archive.
I've added 4 more dll's because I'm not sure which one I have to use:
extension=php_mongo-1.5.4-5.5-vc11-nts
extension=php_mongo-1.5.4-5.5-vc11
extension=php_mongo-1.5.4-5.5-vc11-nts-x86_64
extension=php_mongo-1.5.4-5.5-vc11-x86_64
So now im getting 5 warnings instead of one. At the end I guess one of them will work and I'll delete the other 4.
Things I tried and I'm sure they are ok:
The extension_dir is pointing to the correct folder.
The php.ini that I modified is the one that xammp loads.
Phpinfo dosen't show anything about mongo.
What more can I try ?
Edit
I tried
echo extension_loaded("mongo") ? "loaded\n" : "not loaded\n";
and it always says 'not loaded'.
Edit
Finally!
The problem was the dll's name. It has to be 'php_mongo.dll' and I was trying to load the full name dll as I said at the begining of this post. So I changed the correct dll for me (extension=php_mongo-1.5.4-5.5-vc11) for extension=php_mongo.dll and voilà!

You have not installed MongoDB PHP driver please see this link http://www.php.net/manual/en/mongo.installation.php
Update sources
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update
Install MongoDB PHP Driver
sudo apt-get install php5-dev php5-cli php-pear -y
sudo pecl install mongo
Open your php.ini file and add to it:
extension=mongo.so
Restart apache
sudo /etc/init.d/apache2 restart
Other helping info:
this should help to find your php.ini file:
php -i | grep 'Configuration File'
On Ubuntu it shows this:
Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini
take a note, that you run this command from cli (command line) so for your true php.ini go to folder apache2 instead of cli :)

For those arriving on this page with PHP 7 installed:
The MongoCLient class was provided by pecl install mongo.
But pecl/mongo is not available for php7 and deprecated in favor of pecl/mongodb. But with pecl/mongodb you'll need to use MongoDB\Driver\Manager instead of MongoClient (warning on page says so too).
See here for further reading.
This said, you will need an abstraction layer on top of the PHP MongoDB\Driver\Manager. This is provided by mongodb/mongo-php-library.
You will need to refactor stuff like:
\MongoClient to \MongoDB\Client
\MongoCollection to \MongoDB\Collection
\MongoClient->selectDB to \MongoDB\Client->selectDatabase
\MongoClient->listDBs to \MongoDB\Client->listDatabases
also output is not an array but an iterator, so you'll need to use iterator_to_array, along with edits to how you use the resulting object
\MongoCollection->getName to \MongoDB\Collection->getCollectionName
\MongoCollection->update to \MongoDB\Collection->updateOne or updateMany
\MongoCollection->remove to \MongoDB\Collection->deleteOne
\MongoCollection->batchInsert to \MongoDB\Collection->insertMany

The answer is indeed to follow the instructions.
I was missing the very important require line that must come before creating the new mongodb object:
<?php
require 'vendor/autoload.php';
$client = new MongoDB\Client("mongodb://localhost:27017");
And of course you need to run this command in the root of your project as per the instructions:
composer require mongodb/mongodb

Getting the same error and now it's solved.
I am using Linux Mint. To solve this issue I added extension=mongo.so in two directories:
/etc/php5/cli/php.ini
/etc/php5/apache2/php.ini

install the driver, I have for example php5.6:
sudo apt-get install php5.6-mongo

From this page:
Note: Additional DLL dependencies for Windows Users:
In order for this extension to work, there are DLL files that must be available to the Windows system PATH

your php version and dll file version should be same check it if versions are not same then update your xampp php according to the available dll version.

Related

How to install and activate PDO for PHP 7? [duplicate]

I am trying to setup webserver with PHP 7 RC3 + Nginx on Ubuntu 14.04 (for test purposes).
I installed Ubuntu in Vagrant using ubuntu/trusty64 and PHP 7 RC 3 from Ondřej Surý (https://launchpad.net/~ondrej/+archive/ubuntu/php-7.0).
I can not find the way to install MySQL PDO (PHP sees PDO class but not anything related to MySQL, like PDO::MYSQL_ATTR_DIRECT_QUERY etc.)
Looks like there is no lib php7.0-mysql (by analogy with standard php5-mysqlnd and php7.0-fpm etc. from Ondřej)
Section PDO in phpinfo():
PDO support enabled
PDO drivers no value
How can I get it?
For thoses running Linux with apache2 you need to install php-mysql
apt-get install php-mysql
or if you are running ubuntu 16.04 or higher just running the following command will be enought, no need to edit your php.ini file
apt-get install php7.2-mysql
If you are running ubuntu 15.10 or below:
Edit your php.ini file, it's located at /etc/php/[version]/apache2/php.ini and search for pdo_mysql
you might found something like this
;extension=pdo_mysql.so
Change it to this
extension=pdo_mysql.so
Save the file and restart apache
service apache2 restart
Check that it's available in your phpinfo()
First install php-mysql
sudo apt-get install php7.0-mysql
//change the version number based on the php version
then enable the module
sudo phpenmod pdo_mysql
and restart apache
sudo service apache2 restart
On ubuntu 18.04 following works for me
type this in your terminal
For php7.3
sudo apt-get install php7.3-gd php7.3-mysql
For php7.0
sudo apt-get install php7.0-gd php7.0-mysql
For php5
sudo apt-get install php5-gd php5-mysql
First, check if your php.ini has the extension enabled "php_pdo_mysql" and "php_mysqli" and the path of "extension_dir" is correct. If you need one of above configuration, then, you must restart the php-fpm to apply the changes.
In my case (where i am using the Windows OS in the company, i really prefer OSX or Linux), i solved the problem putting this values in the php.ini:
; ...
extension_dir = "ext"
; ...
extension=php_mysqli.dll
extension=php_pdo_mysql.dll
; ...
I hope this helps.
Since eggyal didn't provided his comment as answer after he gave right advice in a comment - i am posting it here: In my case I had to install module php-mysql. See comments under the question for details.
If you are on windows, and your php folder is not in your PATH, you have set the absolute directory in your php.ini
for example:
extension_dir = "C:/php7/ext"
and uncomment
extension=php_mysqli.dll
extension=php_pdo_mysql.dll
Restart apache2.4 and it should work.
I hope it helps.
download the source code of php 7 and extract it.
open your terminal
swim to the ext/mysqli directory
use commands:
phpize
./configure
make
make install (as root)
enable extension=mysqli.so in your php.ini file
done!
This worked for me
I had, pretty much, the same problem. I was able to see that PDO was enabled but I had no available drivers (using PHP 7-RC4). I managed to resolve the issue by adding the php_pdo_mysql extension to those which were enabled.
Hope this helps!
I resolved my problem on ubunto 20.4 by reinstalling php-mysql.
Remove php-mysql:
sudo apt purge php7.2-mysql
Then install php-mysql:
sudo apt install php7.2-mysql
It will add new configurations in php.ini
Had the same issue, resolved by actually enabling the extension in the php.ini with the right file name. It was listed as php_pdo_mysql.so but the module name in /lib/php/modules was called just pdo_mysql.so
So just remove the "php_" prefix from the php.ini file and then restart the httpd service and it worked like a charm.
Please note that I'm using Arch and thus path names and services may be different depending on your distrubution.
I'll start with the answer then context NOTE this fix was logged above, I'm just re-stating it for anyone googling.
Download the source code of php 7 and extract it.
open your terminal
swim to the ext/pdo_mysql directory
use commands:
phpize
./configure
make
make install (as root)
enable extension=mysqli.so in your php.ini file
This is logged as an answer from here (please upvote it if it helped you too): https://stackoverflow.com/a/39277373/3912517
Context:
I'm trying to add LimeSurvey to the standard WordPress Docker. The single point holding me back is "PHP PDO driver library" which is "None found"
php -i | grep PDO
PHP Warning: PHP Startup: Unable to load dynamic library 'pdo_odbc' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20170718/pdo_odbc (/usr/local/lib/php/extensions/no-debug-non-zts-20170718/pdo_odbc: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20170718/pdo_odbc.so (/usr/local/lib/php/extensions/no-debug-non-zts-20170718/pdo_odbc.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
PHP Warning: Module 'mysqli' already loaded in Unknown on line 0
PDO
PDO support => enabled
PDO drivers => sqlite
PDO Driver for SQLite 3.x => enabled
Ubuntu 16 (Ubuntu 7.3.0)
apt-get install php7.0-mysql
Result:
Package 'php7.0-mysql' has no installation candidate
Get instructions saying all I have to do is run this:
add-apt-repository -y ppa:ondrej/apache2
But then I get this:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 223: ordinal not in range(128)
So I try and force some type of UTF:
LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/apache2
and I get this:
no valid OpenPGP data found.
Follow some other instructions to run this:
apt-get update
and I get this:
Err:14 http://ppa.launchpad.net/ondrej/apache2/ubuntu cosmic/main amd64 Packages
404 Not Found
Err:15 http://ppa.launchpad.net/ondrej/php/ubuntu cosmic/main amd64 Packages
404 Not Found
and - I think because of that - I then get:
The repository 'http://ppa.launchpad.net/ondrej/apache2/ubuntu cosmic Release' does not have a Release file.
By this stage, I'm still getting this on apt-get update:
Package 'php7.0-mysql' has no installation candidate.
I start trying to add in php libraries, got Unicode issues, tried to get around that and.... you get the idea... whack-a-mole. I gave up and looked to see if I could compile it and I found the answer I started with.
You might be wondering why I wrote so much? So that anyone googling can find this solution (including me!).
['class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost:3306;dbname=testdb',
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',]
It's simple:
Just provide the port number along with the host name
and set default sock path to your mysql.sock file path in php.ini which the server is running on.
Just as an update for those working with installing Linux programs, please note, that things such as 'php7.0-common' are now simply known as 'php-common'.

Installing curl to PHP cli

I'm trying to set up a cronjob which requires curl, and I'm calling it directly from crontab with
* * * * * /usr/bin/php myurl/my_cron.php
The problem is, it looks like the curl module isn't installed for my phpcli.
It works just fine when I hit the url from my browser, but when I run
php -q myfile.php
from the command line, it returns
PHP Fatal error: Call to undefined function curl_init() in my_cron.php on line 20
When I run php -m the curl module does not show up. However when I go to the browser and dump the php_info(), the module shows up and says its correctly installed.
The other kicker is i've been trying to install curl with apt-get onto the server (Ubuntu 12.04 php 5.4), it seems to take down my PHP as it begins to simply attempt to download the index.php file wherever I try to browse to.
Here are the attempts I've made to install curl that have taken down PHP:
sudo apt-get install php-curl
sudo apt-get install curl libcurl3 libcurl3-dev php5-curl
After each of these I restarted the apache2 server and still no dice, it attempted to download the file instead of opening the page.
How can I install php5-curl to just the cli, so that my server can run it and I don't have to go through a browser?
The other possibility is I could run the cronjobs through wget from the crontab file, but I've heard that's not the best option and potentially unreliable.
Any help is much appreciated. Thanks!
I had the same issue. But, finally I solved it by running the following command.
sudo apt-get install php7.0-curl
Restart the server after installing. This answer may not be useful for the user who asked because he asked it two months ago. But, this may be useful for the users who reading this in the future.
Here's how I've fixed this on ubuntu 14.04 when curl was working in php files run through apache, but not when called from the cli.
ssh to your server and cd to /
find / -name 'curl.so'
Run the above find command to locate where the curl binary is hanging out at. If you can't find the file, you might need to install curl and run the find command again.
apt-get install php5-curl
You'll now want to edit the php.ini being used for php files run from the cli (it's different than the one used by apache), and is likely at /etc/php5/cli/php.ini
nano /etc/php5/cli/php.ini
You can also run
php -i | grep 'php.ini'
To get the file path, just to be sure.
In your php.ini file search for [curl] by pressing ctrl + w
You'll now want to add the extension to the file and it should look something like the following, though your path to the curl.so file and such might be a little different:
[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
;curl.cainfo =
extension=/usr/lib/php5/20131226/curl.so
After doing the above, I was able to use curl in php scripts run from the cli.
first find the version of your php cli by:
php -v
for example if it was version 7 then:
sudo apt-cache search php7
this will give you the proper module names for your current version:
php7.0-curl - CURL module for PHP <---- the name of curl module.
php7.0-dev - Files for PHP7.0 module development
php7.0-gd - GD module for PHP
php7.0-gmp - GMP module for PHP
php7.0-json - JSON module for PHP
php7.0-ldap - LDAP module for PHP
php7.0-mysql - MySQL module for PHP
.
.
so on
so to add curl support, copy the name of curl module from the list above then do the following:
sudo apt-get install php7.0-curl
If you are using the command-line interface ('cli') for php5, instead of
php -q myfile.php
please use:
php5 -q myfile.php
php5-curl seems to enable the curl module for the cli php5 and not php and both (can) load different configurations and modules.
I use ubuntu 14.04 and php 5.3. After upgrading to php 5.6.29 I also has problem with php curl. My directory structure after updating to php 5.6.29:
/etc/php5 - old version (5.3)
/etc/php/5.6 - new version
The next command
sudo apt-get install php5-curl
didn't help (looks like it connects to old php version - 5.3).
I have found next article: php 5.6 for magento
It advice to use command
apt-get -y install php5.6-curl
instead of
apt-get -y install php5-curl
It works for me!
The first thing you should always check is your php.ini file. You should have a php.ini file in your web root. Curl is installed by default on most web servers; I haven't found a web server with PHP that hasn't already had curl installed. Its not always enabled, though.
Check your your php.ini file and search for php_curl.dll, it should look like this:
;extension=php_curl.dll
Just remove the semicolon (;) from before "extension" and save the file. It should work right away. According to your phpinfo.php its already installed, so it likely just needs to be enabled.
A similar question can be found here if you're interested: Call to undefined function curl_init()
In case someone reached here to find windows version of running curl.
Open php.ini and remove the ; before extension=php_curl.dll around line 656.
I am pretty much sure what Apache loads is C:\wamp\bin\apache\Apache2.2.17\bin\php.ini therefore you may find curl working from browser.
But when php is run from command line then it may show unknown function curl_init();
Run php -r "echo php_ini_loaded_file();" in the command line to see which ini file is being loaded.
Usually its found inside C:\wamp\bin\php\php5.3.5\php.ini its a different file from what Apache is using. So open it and then remove the ; before extension=php_curl.dll around line 656.
Hope it helps someone.

Symfony 2: Install and enable the intl extension

I'm using XAMPP for Windows and decided to try out Symfony 2.
As soon as I get to Symfony Configuration page it recommends me to install and enable intl.
I tried reading PEAR's and PECL's guides, since I'm total 0 on this topic(started learning PHP recently), but nothing worked.
I found php_intl.dll inside my C:\xamp\php\ext. The php.ini's extension_dir= is set to "C:\xampp\php\ext". I was only missing the extension=php_intl.dll inside php.ini, so I added it.
Unfortunately it didn't work, and Symfony keeps asking me for intl.
Whenever you make changes to php.ini, remember to restart Apache. Otherwise the changes won't take effect. You can do this in the XAMPP Control Panel by clicking the stop/start button in the Apache module row.
http://php.net/manual/en/intl.requirements.php
This extension is bundled with PHP as of PHP version 5.3.0.
Alternatively, the PECL version of this extension may be used with all
PHP versions greater than 5.2.0 (5.2.4+ recommended).
also read my comment to your post above
Follow these steps:
Copy all the file names starting with "icu" from C:\wamp\bin\php\php5.5.12 and paste to C:\wamp\bin\apache\apache2.4.9\bin (I am using WAMP. Figure it out yourself for XAMP.)
You can try. Nothing will harm. If you feel the things are not working, you can delete them. I am sure it will work for you 100%.
Uncomment the following line on C:\wamp\bin\apache\apache2.4.9\bin\php.ini
extension=php_intl.dll
It will work.
For Linux users:
Installing php5-intl made it work for me
sudo apt-get install php5-intl
found here:
http://www.php.net/manual/en/intl.setup.php
For OS X users:
1.Normally, the PHP is automatically installed on OSX. So, if you would like to use the XAMPP, or whatever apache server, you must change the path point to XAMPP. You can check the path by using:
$ which php
(do not copy $ sign, it means than you should type this in Terminal, not php)
You should get: /Applications/XAMPP/xamppfiles/bin/php ,
if not, you will get /usr/bin/php.
This is OSX' php. So, you have to change it to XAMPP' php by using:
$ PATH="/Applications/XAMPP/xamppfiles/bin:${PATH}"
2.Run this command to download, unpack, compile and install ICU (you can choose different version is your Symfony required):
$ curl -sS -o /tmp/icu.tar.gz -L http://download.icu-project.org/files/icu4c/57.1/icu4c-57_1-src.tgz && tar -zxf /tmp/icu.tar.gz -C /tmp && cd /tmp/icu/source && ./configure --prefix=/usr/local && make && sudo make install
than run:
$ sudo pecl install intl
and specify where ICU libraries and headers can be found [DEFAULT] :
/usr/local
Pay attention to information in install report:
You should add "extension=intl.so" to php.ini
Don't forget to reboot Apache and check version:
<?php
if (extension_loaded('intl')) {
echo "PHP: " . PHP_VERSION . "<br>\n";
echo "ICU: " . INTL_ICU_VERSION . "<br>\n";
} else {
die('OOPS! The intl extension is not enabled!');
}
Partially taken from here but modified to get success Install PHP Internationalization extension (Intl) on XAMPP on Mac
To activate this extension in Xampp, just edit your php.ini (i.e. in C:\xampp\php directory) and remove the semicolon to uncomment this line :
;extension=php_intl.dll
Eventually don’t forget to restart Apache !
To solve this go to "c:\xampp\php\php.ini" open it in your editor and search for Dynamic Extensions then go down search for ;extension=php_intl.dll and enable it(remove ; to became like ) extension=php_intl.dll after that save your file php.ini and restart your localhost

Install Tidy, PHP

I guess when php was installed on my box --with-tidy arg was not included.
When browsing my php.ini the line:
;extension=php_tidy.dll
is missing.
How can I add the module to my php install? Note I tried adding:
extension=php_tidy.dll
but with no luck.
I checked the docs but all they say is:
This extension is bundled with PHP 5 and greater, and is installed using the --with-tidy configure option.
On Ubuntu, you can add tidy to your php5 installation by running this command in a terminal:
$ sudo apt-get install php5-tidy
If you want to check if it is already installed or validate that the command above worked:
$ php -i | grep libTidy
libTidy Release => 25 March 2009 // means tidy is installed
To install both php5 and tidy at the same time, use this command (as an alternative to the "--with-tidy" configuration option):
$ sudo apt-get install php5 php5-tidy
For PHP 7 just run
sudo apt-get install php-tidy
And restart Apache, That's it :)
Update: Thanks to #Arnis Juraga
For the version above PHP 7, you can mention the version code as follows.
apt-get install php7.4-tidy
I was having the same trouble, and wrote up my solution here:
Installing Tidy 2.0 PHP extension on Ubuntu 10.10
http://web.onassar.com/blog/2010/11/16/installing-tidy-2-0-php-extension-on-ubuntu-10-10/
It works on newer versions of Ubuntu as well.
While it may not be the most direct route, it works well.
I hope it helps others.
Put that line in php.ini next to the other extension=...dll lines and make sure php_tidy.dll is in the directory configured in php.ini under extension_dir.
In ubuntu: you need to run the following command in terminal
sudo apt-get install php5-tidy
and the restart the server
sudo service apache2 restart.
For those who are having trouble enabling Tidy extension for PHP (using PHP handler for Apache server) on Windows (WAMP server), please note that you may be editing the wrong php.ini file. Assuming that WAMP server is installed in C:\wamp[ver]\ where [ver] is a variable version number.
File that you're probably editing:
C:\wamp[ver]\bin\php\php[ver]\php.ini
File that you must edit:
C:\wamp[ver]\bin\apache\apache[ver]\bin\php.ini
You also might want to make sure what's the correct php.ini file to edit by creating an info php file and add this code to it:
<?php phpinfo();
Then look for Loaded Configuration File string.
Finally make sure you uncomment this line within the correct php.ini file:
extension=tidy

pgsql.so is not loaded in PHP

I've been tasked to create a PHP app which accesses an existing PostgreSQL database. This is my first time working with Postgre, not to mention the PHP has already been installed in the Linux box on which the app is supposed to run. I have no experience setting up this stuff, I just code.
My question is that I can't seem to get the Postgre extension working in PHP. I checked the php.ini file, there were no "extension=..." lines. So I added "extension=pgsql.so". I then checked the "extension_dir" and found that there were only 2 files in there (ldap.so, phpcups.so), I added a pgsql.so file taken from another Linux box. I restarted httpd. And it does not work. I couldn't find any "pgsql" or "postgre" in phpinfo().
Forgive my noobness. I know too little Linux. I would really appreciate it if you can point me to the right direction.
I used the suggestion given by number5:
Dude, I'm on RedHat. I used the "yum" version of the command you gave, and I got this:
[root#perseus ~]# yum install php-pgsql
Loading "installonlyn" plugin
Setting up Install Process
Setting up repositories
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=5&arch=x86_64&repo=extras error was
[Errno 4] IOError:
Error: Cannot find a valid baseurl for repo: extras
UNFORTUNATELY, the Linux server I'm using is NOT connected to the Internet. Any other way to install?
It depends on which Linux distro you are using.
If you are using Ubuntu/Debian, you need to:
sudo apt-get install php5-pgsql
Fedora/CentOS
yum install php-pgsql
usually you can find out which distro you are on by:
ls /etc/*-release
On CentOS extensions create separate *.ini file one per each php extension in /etc/php.d
So, don't alter main *.ini file, but create /etc/php.d/pgsql.ini and add there a line
extension=pgsql.so
Then you will need to restart Apache using
service httpd restart
But the best automated way is to just type
yum install php-pgsql
In your case that didn't work because of some problems in yum configuration. Go to /etc/yum-repos.d
Type
nano /etc/yum.repos.d/CentOS-Base.repo
Scroll down to [extras] section and ensure it is like this:
#additional packages that may be useful
[extras]
priority=1
name=CentOS-$releasever - Extras
=extras
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
Correct it if needed, save file (Ctrl-X) and do the following:
yum clean all
yum upgrade
Afterwards try to repeat
yum install php-pgsql
Remove the .so file you've copied from the other machine (though it might work, there's no need to take the risk)
Use the distribution's package manager to install the php_pgsql/php5_pqsql module
Restart the apache and try again. Maybe the module has been added to an .ini file automagically
If not, run <?php echo 'ini: ', get_cfg_var('cfg_file_path'); to see which php.ini you have to edit
edit this ini file
restart the apache
My operation system Linux mint kde and there was same issue
pg_connect()
You must install if you are use php5.6
sudo apt-get install php5.6-pgsql
After you must change extention name in "php.ini" file.
;extension=php_pgsql.dll
'dll' to 'so'
For example
extension=php_pgsql.so

Categories