Laravel 5.2 PDO Exception on terminal - php

I am trying to recreate a downloaded site with Laravel 5.2 in my VPS and I am getting a PDO Exception when I run php artisan command on the terminal:
I checked that MySQL was installed with sudo apt-get install php5-mysql and it has the latest version. I also sought in phpinfo() and I got the following information:
So, any clues?

The PHP CLI often uses a different php.ini to the one used by the web server.
Do a php -i | grep -i Loaded Configuration File
Then check the php.ini file and if this line is commented with a ; remove the ; so the line looks like this
;extension php_pdo_mysql.so
Change to
extension php_pdo_mysql.so
This will activate the PDO extension

Related

How to execute PHP CLI script with extension on synology

I'm trying to execute a php script via command line on my synology NAS. Via web browser it's working fine. However, via CLI I'm getting an error although I loaded the extensions in /etc/php/php.ini.
Fatal error: Uncaught PDOException: could not find driver in /volume1/web/blabla.php:16
Any ideas?
Synology uses two different php intallations. One for the internal issues (like administrator panel) and the other from the package you've installed for the web server.
you can observe the differences running from command line:
php --ini
php56 --ini
php7 --ini
(depending of the php package installed)
use php56 or php7 to run your script
Update
Please use the following for more recent versions of PHP (assuming both are installed)
php72 --ini
php74 --ini
I was in the same boat–trying to get PHP 7.0 to work on the command-line of my Synology DiskStation DS218+.
I typed php70 --ini and I found out that PHP was using an INI file located at /usr/local/etc/php70/php.ini.
I went to the directory
cd /usr/local/etc/php70/
Made a backup of the .ini file just incase.
sudo cp php.ini php.ini.bak
Opened vi.
sudo vi php.ini
Hit i to enter Insert mode. I changed from this:
extension_dir = "/usr/local/lib/php70/modules"
to this:
extension_dir = "/volume1/#appstore/PHP7.0/usr/local/lib/php70/modules"
I also added this line:
extension = pdo_mysql.so
I then hit Esc, followed by : then wq and . This leaves insert mode, writes the .ini to disk, and quits.
That's it! After that, I was able to run command-line scripts by invoking php70 followed by the script name.
Just make sure to use the correct PHP version and PHP.ini. You can find the location of your php.ini by typing "php --ini". You can also do that on the executables php56/php70. They all have separate php.ini files. Additionaly make sure to load the correct extension directory in the applicable php.ini file.
for me (DSM 6.1.6-15266 Update 1) the following helped:
Add the following lines to /usr/local/etc/php70/php.ini
extension = pdo_mysql.so
extension = openssl.so
I struggled very long to get the correct values, finally I found them by comparing to the config files of the php56 instance.
Did you try: php56 path/to/script.php or php70 path/to/script.php ?
If I'm not wrong, /etc/php/php.ini is for DSM engine...
For symfony, I use php56 bin/console command. php or php70 doesn't work for me (DSM 6.1.3 on DS415+)
Hope it helps !

Enable MCrypt using MAMP

I'm using MAMP and ive installed a fresh version of Opencart, its telling me i dont have MCrypt enabled, when i go to terminal and type:
php -m | grep mcrypt
output: mcrypt
I can locate the library but it doesn't seem to be enabled.
That fact that php -m | grep mcrypt returns mcrypt, means the mcrypt library is INSTALLED and ENABLED.
Although it may just be enabled for CLI.
You can try editing the PHP.ini file and adding the following line under the ; Extensions section:
extension=mcrypt.so
Restart Apache / MAMP after saving php.ini file.
To find the correct php.ini file to edit, run the following command line:
php --info | grep php.ini
(If the line already exists, you may just need to remove the ; thats in front of it.)
I have tried so many ways but had no luck.
After a lot of trials finally came up with a solution.
Go to bin directory inside current active PHP version directory. In my case it is /Applications/MAMP/bin/php/php7.2.8/bin
It might be different in your case. Now run the below command with sudo
sudo ./pecl install channel://pecl.php.net/mcrypt-1.0.1
Now You should add extension=mcrypt.so to php.ini
Restart MAMP Service and check if it is working.
I had this issue following upgrading to MAMP 5.1 and using PHP 7.1.20...
The issue I found was not that MAMP PHP did not have mcrypt installed, it certainly does come bundled.
The issue was that the MAMP PHP configuration option "Make this version available on the command line" was NOT working and so the version of PHP I was using on the command line [in my case] was the macOS default PHP 7.1.16 without mcrypt (the version included in macOS by default)
Reverting to the old cli php alias meant the correct MAMP version of PHP was used on the command line
Added to .bash_profile
alias php='/Applications/MAMP/bin/php/php7.1.20/bin/php'
try in console
pecl install mcrypt
Using Brew:
Install mcrypt: brew install mcrypt
In Mamp: File -> Edit Template -> PHP (php.ini)-> {PHP version}
Find 'Dynamic Extensions' in the text and add the following below (after the lines starting with ';'):
extension=mcrypt.so
Save, restart and test (with php -i | grep mcrypt for example)

php artisan migrate : PDOException could not find driver

I was in need to install Php 5.6 with postgres. I followed the instructions from here and did:
brew install php56 --without-mysql --without-apache --with-postgresql
brew install php56-pdo-pgsql
Php 5.6 and postgres are running fine with my Projects using PostgreSQL.
Now there is some problem with Laravel projects using mysql. On running php artisan migrate command, it shows:
[PDOException]
could not find driver
Project works fine in the browser though. Data is being fetched perfectly from the mysql tables. php -m also does not shows mysql. But phpinfo does shows mysql module.
What could be the problem? Do i need to reinstall mysql?
Probably, you would need to activate mysql extension in php
Find your php.ini. Create a php file with following content and run it. It will tell you where it is.
<?php phpinfo(); ?>
Just look at the path of loaded configuration file. Common places include /etc/apache/, /etc/php4/apache2/php.ini, /etc/php5/apache2/php.ini or even /usr/local/lib/php.ini for Windows it may be C:\Users\username\PHP\php.ini
Edit your server’s php.ini and look for the following line. Remove the ‘;’ from the start of the line and restart Apache. Things should work fine now!
;extension=mysql.so
should become
extension=mysql.so
For windows it will be
;extension=mysql.dll
should become
extension=mysql.dll
Restart your apache service to load the new configuration.

Class 'MongoClient' not found

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.

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.

Categories