PDOException could not find driver on Zend Framework 2 + Doctrine - php

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!

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.

Doctrine fails to connect to MYSQL database in linked docker container

I am using two Docker container, one having ubuntu and an apache webserver running, the other one a mysql server. The containers are linked and i can connect from the ubuntu container onto the mysql server. For the connection I use in the ubuntu container:
mysql -u root -h mysql
where the second 'mysql' is the name of the container. I can connect to it through the container id as well, so the connection works as well as connecting onto the database from the windows environment.
What doesnt work is the connection from doctrine to the database within the PHP application which is in the ubuntu container.
The config looks like this:
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'mysql',
'port' => '3306',
'user' => 'root',
'password' => '',
'dbname' => 'db_name',
'charset' => 'utf8',
)
)
),
)
But I get the Error message
Uncaught PDOException: could not find driver in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php
and
Zend\ServiceManager\Exception\ServiceNotCreatedException: An abstract factory could not create an instance of doctrine.entitymanager.ormdefault(alias: doctrine.entitymanager.orm_default). in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php
Does anyone have any idea how to solve this and where exactly the error comes from?
I have already tried to put in the container ID as 'host' and commented out the 'password' field as it is not used.
Thanks in advance
Jonathan
You need to have pdo_mysql allowed on your system, it doesn't seem a connectivity issue but a php configuration problem.
Can you try to do this command inside your php container
php -i | grep pdo_mysql
Just to understand if php has this module

ZF2+Doctrine2 - Suddenly getting error on Mac: "Doctrine\ORM\EntityManager"; no instance returned

I am working on building a Zend Framework 2 (ZF2) website using Doctrine2 DBAL/ORM, where I develop equally on Windows and Mac, sometimes Linux (Ubuntu) (I like the all-around experience).
In composer.json I have the following versions:
"doctrine/doctrine-orm-module": "^0.9.1"
"zendframework/zendframework": ">=2.3.2,<3.0.0"
At first, my website ran fine on all three environments using XAMPP (Apache 2.4.16, MySQL 5.0.11, and PHP 5.6.12). But suddenly, after I performed some changes on Windows and pulled those changes from GitHub to my Mac computer, Doctrine started failing with the following exception message:
An exception was raised while creating "Doctrine\ORM\EntityManager"; no instance returned
I got this exception a lot while configuring Doctrine2 in ZF2. But once I was done, things just worked. That is until it broke on Mac, only! It still works fine on Windows.
I have cleared the cache completely (rm data/cache/*) and I have verified the integrity of my configuration files. The only difference in configuration between Windows and Mac is that I provide a unix_socketpath for MySQL (see below).
My config/application.php:
<?php
return array(
'modules' => array(
// ...
'DoctrineModule',
'DoctrineORMModule',
),
'module_listener_options' => array(
// ...
),
);
My config/autoload/databases.local.php looks like this (with changed values for database server login information):
<?php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'unix_socket' => '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock', // This is for Mac
'host' => 'some_host',
'port' => '3306',
'user' => 'some_user',
'password' => 'some_password',
'dbname' => 'some_database',
)
)
),
'configuration' => array(
'orm_default' => array(
'proxy_dir' => 'core/server/data/DoctrineORMModule/Proxy',
'proxy_namespace' => 'DoctrineORMModule\Proxy',
)
)
),
);
As mentioned, these configurations work on Windows (I can retrieve and use the EntityManager) and is a 1:1 mirror onto my Mac solution. So what happens?
When I check the cached configuration file, data/cache/module-config-cache.application.config.cache.php, on my Mac, the database login informations are incorrect, having the following values:
// ...
array (
'host' => 'localhost',
'port' => '3306',
'user' => 'username',
'password' => 'password',
'dbname' => 'database',
),
// ...
Obviously these are wrong. But how can ZF2 suddenly generate incorrect configurations on Mac?
I even tried checkout out previous commits from Git on my Mac, which I know worked for certain. But the same problem occurs. The only difference, I suspect might have influence on this behavior, is that I ran composer.phar update, which might've changed the versions of ZF2 and/or Doctrine2.
The problem boiled down to the glob pattern for the autoload config files being incorrect.
$appConfig['module_listener_options']['config_glob_paths'][$index] = getcwd() . '/' . $path;
getcwd() had an incorrect pointer, despite having specified the following:
define('ROOT_PATH', realpath(__DIR__ . '/../../../..'));
chdir(ROOT_PATH);
For some reason I cannot fathom, it worked before on Windows and Ubuntu, but not on Mac. Now it works in all three environments.
It's an almost invisible error and required line-by-line debugging through the bootstrapping logic.
In my case, this error message were thrown, when the database was missing!

How to connect Cakephp 1.3 with MS SQLServer 2008?

I can't connect to MSSQL Server. I've been researching for 2 days, yet I can't find any useful resources. This is the error I'm getting:
URL rewriting is not properly configured on your server.
Help me configure my database. I don't / can't use URL rewriting:
Your tmp directory is writable.
The FileEngine is being used for caching. To change the config edit APP/config/core.php
Your database configuration file is present.
Here's my database config file:
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mssql',
'persistent' => false,
'host' => 'Charmae-PC\Charmae',
'login' => 'sa',
'password' => 'pass',
'database' => 'obbm',
'prefix' => '',
'port' => '',
);
}
What should I do?
You need to configure your database with cakephp[windows php driver]. I don't know which version cakephp are you using. But for any version of cakephp and wampserver windows php drivers are different. for more information
Download the sqlsrv driver from here
and then the rest of configuration.
By the way you have to use sqlsrv driver for config/database.php
var $default = array(
'driver' => 'sqlsrv',
...

CakePHP: Can't access MySQL database

I'm new to CakePHP and am just running through the configuration process, but am stumped why Cake can't access my MySQL database. The Cake info page says my tmp directory is writable, the FileEngine is being used for caching (don't know what this means), and my database configuration file is present, but that CakePHP cannot connect to the database.
Here are my setup details:
PHP 5.3 (pre-installed on Snow Leopard)
MySQL 5.1.40 64-bit
CakePHP 1.2.4.8284
Here are the steps I went through:
Created a MySQL schema called cake_blog
Created a MySQL user called cake_blog_user
Granted cake_blog_user the appropriate permissions on cake_blog#localhost and cake_blog#%
Copied the database.php.default file to database.php and edited the database connection details as appropriate
Here is the relevant configuration data from database.php:
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'cake_blog_user',
'password' => 'cake_blog_password',
'database' => 'cake_blog',
'prefix' => '',
);
Am I missing something here? I should also mention that if I insert an echo mysql_error(); into the /cake/libs/view/pages/home.ctp file right before it tests the database connection, the error displayed is "No such file or directory." I have no idea what file or directory it's talking about.
Thanks!
What usually bites me in it's that MySQL thinks of 'localhost' as 'connect thru the unix socket' and '127.0.0.1' 'connect thru TCP port'. With things like XAMPP (at least on mac) the unix socket file isn't there. Just use 127.0.0.1 instead.
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => '127.0.0.1',
'login' => 'cake_blog_user',
'password' => 'cake_blog_password',
'database' => 'cake_blog',
'prefix' => '',
);
Should work all the time.
If it is the socket, just edit /etc/php.ini to reflect the following
pdo_mysql.default_socket=/tmp/mysql.sock
and
mysql.default_socket = /tmp/mysql.sock
I believe you can also do the following
<?php
public $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'cake_blog_user',
'password' => 'cake_blog_password',
'database' => 'cake_blog',
'prefix' => '',
'port' => '/tmp/mysql.sock',
)
?>
doing this might mean you need to edit the database.php file when you go live on the production server.
Thanks everyone for pointing me in the right direction. The mysql.sock file has been moved to /tmp/mysql.sock instead of its default location at /var/mysql/mysql.sock. Editing the php.ini file to reflect this has fixed the problem.
check your phpinfo and use the socket listed. that worked for me.
On Ubuntu, if you installed both 7.0 and 5.6 versions of PHP this wont work.
Youll need to switch if you have both versions:
Look first if is 7.0 version: the command is php -v.
Next do
sudo a2dismod php7.0
sudo a2enmod php5.6
sudo service apache2 restart

Categories