YII 2.0 with PHP 7.0.2 YII db exception - php

I am new with YII 2.0 and i have installed new PHP7.0.2 Version, i was successfully installed the YII2.0 via composer and i enabled my gii code creation.
So that now i want to create a model file for my table users, when i click the model creation via gii page, it showing the "Database Exception – yii\db\Exception".
Can you please suggest your answer for rectify this problem.

The db.php configuration file must define db which looks like the following:
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=myDb',
'emulatePrepare' => true,
'username' => 'root',
'password' => 'rootPassword',
'charset' => 'utf8',
),
OR
Edit php.ini to enable each MySQL-related extension such as the following:
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_pdo_mysql.dll

Related

PDOException with message 'could not find driver' Error for Laravel and Postgresql

First of all, the problem is about Laravel not postgresql or PHP. I can connect postgresql with a simple PHP file. But Laravel can't do it somehow.
When I try to connect postgresql server in my computer with laravel I get "PDOException with message 'could not find driver'" error. I am getting this error when I run DB::connection()->getPdo(); command at artisan tinker.
If I run php artisan migrate command, the error is Illuminate\Database\QueryException : could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations and table_type = 'BASE TABLE')
My configuration is below:
Windows 10
Wamp Server 3.1.4
Apache 2.4.35
PHP 7.2.10
Laravel Framework 6.0.3
Related lines of Laravel .env file:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=laravel_dnm1
DB_USERNAME=postgres
DB_PASSWORD=mrd.BE.265
Related lines of Laravel database.php file:
'default' => env('DB_CONNECTION', 'pgsql'),
...
'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],
When I run print_r(PDO::getAvailableDrivers()); at my server I get below output:
Array ( [0] => mysql [1] => pgsql [2] => sqlite )
Related lines of php info is below:
NOTE: There is no problem when I use mysql instead of postgresql.
NOTE2: I can connect the DB when I use regular PHP. Only Laravel gives this error.
install postgresql
sudo apt-get install php-pgsql
then uncomment pgsql and pdo-pgsql extensions in etc/php/$PHP_VERSION/apache2/php.ini file
then restart apache2
Verify your PHP version with php -v
Install php7.2-pgsql when needed.
I solved the issue. It is a bug at WAMP I think. When I edit php.ini from WAMP's menu it opens a different php.ini than active PHP version's. So I opened php.ini from file system and edited it from there.
There are different php version installed on your WAMP SERVER. when you click on WAMP icon it will show that pgsql and pdo_pgsql are active but for different php version. You need to check your php version using php -v command and then go to the WAMP folder and find that php version and edit php.ini file and enable pgsql extensions there.

Yii2 exception 'yii\db\Exception' with message 'could not find driver'

I have an advanced yii2 template. I'm trying to create a console command.
I've created a controller class and action incide console/controllers folder:
namespace console\controllers;
use yii\console\Controller;
class WorkModelController extends Controller
{
public function actionValidate(){}
}
My action should connect with mysql database, select some data and do something with it. When I running the command: yii work-model/validate I get this error:
C:\OSPanel\domains\localhost>yii work-model/validate Exception
'yii\db\Exception' with message 'could not find driver'
in
C:\OSPanel\domains\localhost\vendor\yiisoft\yii2\db\Connection.php:56
My console/config/main.php and main-local.php files contain next db-config:
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbname',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
];
When I trying connect to the database from another part of app all works fine, but when I trying connect from console I get this error.
Please, help me slove this error.
try to run php -me from the cli and make sure pdo_mysql is there..
if not, then u need to enable it from your php.ini config.
Sometime, when u use server stack such as lamp/xampp, u probably missed out from resync your environment path to use the same version of php.ini of your server stack. to do this, u may simply check/compare php.ini path from both phpinfo() from the browser and php -i from the cli

setup PostgreSQL with Laravel in MAMP

I am using MAMP on my MAC. As it comes with MySQL by default. But now I need to use PostgreSQL in one of my project. How can I setup postgreSQL in MAMP for Laravel project?
Ok if you are set on using postgreSQL over mySQL that comes with MAMP you have to manually install postgreSQL on you location machine the OSX packages can be found here,
If you don't want to do a full install i recommend this Postgres App just download an extract to your applications folder then when you launch it the port number will be displayed in the menu bar like so:
create a database:
go to menu above Click on Open psql
In the command line create you database like so: CREATE DATABASE your_database_name;
should return CREATE DATABASE
Tip to exit postgreSQL CLI use \q then ENTER
You now need to plug these settings into Laravels configuration:
open file: %laravel_directory%/app/config/database.php
in the array replace 'default' => 'mysql', with 'default' => 'pgsql',
now with the information from before edit the 'connections' array like so:
'connections' => array(
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'your_database_name',
'username' => '',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
)
save file
You should now have a functioning database that Laravel can talk to.
Note you do not need the username or password in database config if you are using the Postgres App.

PDOException could not find driver on Zend Framework 2 + Doctrine

I'm trying to install Zend Framework 2 + Doctrine from this manual and have some problem with PDO driver. Doctrine is trying to connect to my MySQL server and then trying to create schema:
./vendor/bin/doctrine-module orm:schema-tool:create
I have this error:
[PDOException]
could not find driver
This is my config/autoload/doctrine.local.php:
return array( 'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' =>'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => 'password',
'dbname' => 'blog',
)))));
I have PHP 5.3.5 and i have uncommented ;extension=php_pdo_mysql.dll in php.ini, also it wasn't commented. But php -m | grep -i pdo gives me:
PDO
only. I'm know that it should be pdo_mysql too, but i'm trying everything and it isn't appear.
Anyway, PDO driver is works well at my another project on this server, where I'm not using Zend and Doctrine.
Ohh yes. I'm just solved a problem. I'm going to this page and thought to find my php.ini files anywhere else perhaps PHP folder - it was one more in "C:\Users\%username%\AppData\Local\VirtualStore\Program Files (x86)\wamp\bin\php\php5.3.5" and there row
;extension=php_pdo_mysql.dll
weren't uncomment. I'm uncommented it and it works!

Cake Console won't work on Mac OS X Lion

I just downloaded CakePHP 2.1 and did all the setup for the database and all the rest, but I'm unable to use the cake console, I get the following error:
Error: Database connection "Mysql" is missing, or could not be created.
If I open the site on my browser I see this:
This is my database.php:
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => '127.0.0.1',
'login' => 'user',
'password' => 'password',
'database' => 'database_schema',
'prefix' => '',
'encoding' => 'utf8',
'port' => ' /Applications/xampp/xamppfiles/var/mysql/mysql.sock',
);
I'm using XAMPP 1.7.3, I read that the issue might be related to PDO, but I have no idea how to set it up properly, any suggestions?
You should enable the php_pdo_extension in php.ini. The file is located at /Applications/XAMPP/etc/php.ini by default.
Mine was Mac OS Yosemite ,MAMP PHP 5.6.1 and spent almost two days trying all the fixes available..finally it was something do with php.ini extension_dir
Old value was '.../no-debug-non-zts-20121212' which was not there in the path specified , so i changed the path to available directory as below...and success!!
php.ini located in MAMP - /Applications/MAMP/bin/php/php5.6.1/conf (This can also be found using phpinfo() under 'Configuration File (php.ini) Path')
fixed path in php.ini
; Directory in which the loadable extensions (modules) reside.
extension_dir = "/Applications/MAMP/bin/php/php5.6.1/lib/php/extensions/no-debug-non-zts-20131226/"

Categories