When i try connect to mysql with clear PHP, its working fine.
My code
$link = mysql_connect('hostname', 'username', 'password');
if (!$link) {
die('Could not connect');
}
if(mysql_select_db('dbname')){
echo 'Connected successfully';
}
But when im trying to connect with yii, then getting the error
My config/main.php
'db'=>array(
'class'=>'CDbConnection',
'connectionString' => 'mysql:host=hostname;dbname=dbname',
'emulatePrepare' => true, /* I try false too*/
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
),
This is output for exception what i print in open() function framework/db/CDbConnection.php
Exception handle here
protected function open()
{
if($this->_pdo===null)
{
if(empty($this->connectionString))
throw new CDbException('CDbConnection.connectionString cannot be empty.');
try
{
Yii::trace('Opening DB connection','system.db.CDbConnection');
$this->_pdo=$this->createPdoInstance();
$this->initConnection($this->_pdo);
$this->_active=true;
}
catch(PDOException $e)
{
echo '<pre>';
var_dump($e); die;
if(YII_DEBUG)
{
throw new CDbException('CDbConnection failed to open the DB connection: '.
$e->getMessage(),(int)$e->getCode(),$e->errorInfo);
}
else
{
Yii::log($e->getMessage(),CLogger::LEVEL_ERROR,'exception.CDbException');
throw new CDbException('CDbConnection failed to open the DB connection.',(int)$e->getCode(),$e->errorInfo);
}
}
}
}
Exception text
"SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client"
I see in display
CDbException
CDbConnection failed to open the DB connection.
My PHP VERSION 5.5.36 Mysql version 5.5.35
My Hosting is i-page dot com
Yii Version '1.1.13'
Thanks for help.
It appears to be a problem with the way the passwords are hashed and the version of MySQL and the MYSQL_PDO library.
Yii uses PDO to query the database, thats why clear PHP works like a charm and Yii doesn't.
To verify this, try this:
$mysqlConnection = new PDO("mysql:host=hostname;dbname= dbname", "username", "password");
this line should throw the following error:
PDO::__construct(): The server requested authentication method unknown to the client [mysql_old_password].
This error is the equivalent to the MySQL 2045:
"SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client
If you confirm that the problem is related to PDO you have several options but you need to access the hosting system (or ask them to fix the problem):
Sign in to MySQL and execute the following command SET PASSWORD FOR 'username'#'hostname' = OLD_PASSWORD('password'); (this will fix the hashing of the pasword)
Upgrade the MYSQL PDO Library (PDO_MYSQL) to match the version of MYSQL on the server.
I got this problem with Yii 1.1.x project and almost got crazy:
First make sure you have
sudo apt install php-xml php-mbstring php-pdo php-mysql
installed, and restart apache
sudo apachectl restart
or:
sudo service apache2 restart
Detailed error was that database library cannot be found.
My solution was related to caching:
cache' => array(
'class' => 'system.caching.CDbCache',
//'class' => 'system.caching.CFileCache',
'connectionID'=>'db', // <<< THIS IS THE ISSUE
),
if connectionID is not set, db caching defaults to mysqli database in /protected/data directory which cannot be accessed if mysqli driver is not installed on system (common issue with dedicated servers, DO droplets, xampp/wamp...)
or, you can disable db caching and enable fileCache instead.
Related
I have this issue with trying to connect MS SQL server from laravel project to get some data from table MECommssion I have created the command name test.php in the handle function of the below code
* Execute the console command.
*
* #return int
*/
public function handle()
{
$crmData = DB::connection('core')->table('dbo.MECommssion')->get();
dd($crmData);
}
In config/database.php
'core' => [
'driver' => env('DB_CONNECTION_SYNTELLICORE'),
'host' => env('DB_HOST_SYNTELLICORE'),
'port' => env('DB_PORT_SYNTELLICORE'),
'database' => env('DB_DATABASE_SYNTELLICORE'),
'username' => env('DB_USERNAME_SYNTELLICORE'),
'password' => env('DB_PASSWORD_SYNTELLICORE'),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],
In .env I have add the login
DB_CONNECTION_SYNTELLICORE=sqlsrv
DB_HOST_SYNTELLICORE=192.168.4.59
DB_PORT_SYNTELLICORE=1433
DB_DATABASE_SYNTELLICORE=test
DB_USERNAME_SYNTELLICORE=MSSB
DB_PASSWORD_SYNTELLICORE=mypassword
Every time I call the command I got this error in terminal
Illuminate\Database\QueryException
could not find driver (SQL: select * from [dbo].[MECommssion])
at E:\laragon\www\InfoPortal\Backoffice\vendor\laravel\framework\src\Illuminate\Database\Connection.php:760
756▕ // If an exception occurs when attempting to run a query, we'll format the error
757▕ // message to include the bindings with SQL, which will make this exception a
758▕ // lot more helpful to the developer instead of just the database's errors.
759▕ catch (Exception $e) {
➜ 760▕ throw new QueryException(
761▕ $query, $this->prepareBindings($bindings), $e
762▕ );
763▕ }
764▕ }
1 E:\laragon\www\InfoPortal\Backoffice\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDOException::("could not find driver")
2 E:\laragon\www\InfoPortal\Backoffice\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDO::__construct("dblib:host=192.168.4.59:1433;dbname=test;charset=utf8", "MSSB", "mypassword", [])
What I trying to do is
1- install ODBC drivers 11, 17 and 18.
2- adding DLL extension for PHP 8.1.10php_pdo_sqlsrv_81_ts_x64 and php_sqlsrv_81_ts_x64.dll and enable the extension from php.ini.
3- restart the server.
4- restart the machine.
5- For every change in .env I re-clear the catch.
6- trying the connection in a separate PHP file use PDO out of laravel project below code it is working with no problem and it shows data.
<?php
$servername = "192.168.4.59,1433";
$username = "MSSB";
$password = "mypassword";
try {
$conn = new PDO("sqlsrv:Server={$servername};Database=test", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->query("SELECT * FROM dbo.MECommssion");
$user = $stmt->fetch();
print_r($user);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: ". $e->getMessage();
}
7- install laragon server on another machine and install the new laravel project same version 9 and create the same command and try the same thing in the new project it is working normally with no problem.
8- composer dump autoload
This is all that I tried, I have no idea why the project did not read the drive sqlsrv while the separate PHP file can connect and get the data.
I would appreciate any help you can provide.
Thanks for help.
I have found the problem it was in the composer
PHP version was running PHP in the WAMP server on the same machine, after removing the WAMP server and re-install the composer in laragon server in PHP 8.1.10 folder, Now all working fine
I have this PHP function which has been working very well until i reinstalled my dev system:
function connectDB($database, $username, $password) {
$DSN = "mysql:host=localhost;dbname=$database";
try {
$DBH = new PDO($DSN, $username, $password); <--- LINE 10
return $DBH;
}
catch(PDOException $e) {
die("Could not connect to the database.\n");
}
}
And i'm getting the error:
PHP Fatal error: Class 'PDO' not found in /var/www/im/tools.php on line 10
I checked phpinfo() and PDO is enabled:
PDO drivers : mysql
PDO Driver for MySQL version: 5.1.54
The interesting thing is that the interaction with th MYSQL database is ok, but i'm still getting the error when debugging.
I'm puzzled about this error! My system is Ubuntu 11.04 + NGINX + PHP 5.3
Any tip to get rid of it? Thanks!
Are you using namespaced code? Maybe you need to use \PDO class then?
This can also happen if there is a php.ini file in the web app's current working directory. If one has been placed there to change certain settings, it will override the global one.
To avoid this problem, don't use a php.ini change settings; instead, do this in the vhost declaration or a .htaccess file with 'php_flag'.
See also PHP Fatal error: Class 'PDO' not found
try
yum install php-pdo
service httpd restart
There are a few similar questions that I have read through and followed the advice but to no end, and only being fairly new to this, I figured now was a good time to stop 'trying' things in case I break anything any further.
I'm receiving the following error when trying to connect to my database via PDO:
Connection error: could not find driver
1. I've ensured that Apache is linked to my homebrew PHP.
$ which php
/usr/local/bin/php
2. Have uncommented the following in php.ini
extension=php_pdo_pgsql.dll
The pdo_pgsql module shows up in php_info
3. My database connection is:
<?php
class Database
{
public $conn;
public function getConnection()
{
try {
$this->conn = new PDO("postgres://$user:$pass#$host:$port/$db_name");
print_r($this->conn);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->conn->exec("set names utf8");
} catch (PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
exit();
}
return $this->conn;
}
}
I've triple checked these details and they are correct (albeit ommitted). I can connect with the same URI via IntelliJ's Database connection Wizard
4. I’ve edited /usr/local/var/postgres/postgresql.conf to include:
#listen_addresses = '*'
I'm still not having any luck and am looking at some guidance in this head scratcher.
As I see you are using Linux, but tried enable .dll library which is used for Windows machines. It makes sense to comment this line.
Make sure that you have pdo_pgsql module enabled:
# php -m | grep pdo_pgsql
pdo_pgsql
Install it if it is not
# yum install php-pgsql
Here is steps that I did to make PHP+PostgreSQL work on my clean CentOS 7 install:
Install PostgreSQL (but I think you already have this installed and
configured)
# yum install postgresql-server postgresql-contrib
Updated config /var/lib/pgsql/data/pg_hba.conf, changed from ident to md5
host all all 127.0.0.1/32 ident
host all all ::1/128 ident
After
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
Restart postgresql service
# service postgresql restart
Install PHP and PDO connector
# yum install php php-pgsql
Here is an example of PHP script I used to test connection:
<?php
// Configure DB Parameters
$host = "localhost";
$dbname = "masterdb";
$dbuser = "automation";
$userpass = "fGmK4hvDZPB6fr6c";
$dsn = "pgsql:host=$host;port=5432;dbname=$dbname;user=$dbuser;password=$userpass";
try{
// create a PostgreSQL database connection
$conn = new PDO($dsn);
// display a message if connected to the PostgreSQL successfully
if($conn){
echo "Connected to the $dbname database successfully!";
echo "\n";
}
}catch (PDOException $e){
// report error message
echo $e->getMessage();
}
And the output:
# php pdo_test.php
Connected to the masterdb database successfully!
This is how I am attempting to connect to the database on heroku:
//db.php
$config = array(
'username' => 'username',
'password' => 'password',
'connection_string'=> sprintf('mongodb://%s:%d','EXAMPLE.herokuapp.com','27017')
);
$connection = new \MongoClient($config['connection_string']);
When I pushed to the repo, and go to the app URL the follow error message is shown:
Failed to connect to: EXAMPLE.herokuapp.com:27017 Connection refused in db.php.
How should I format the connection_string so that it will not give the error message?
There is no locally running MongoDB available on Heroku.
You need to use one of the add-ons at https://addons.heroku.com/#data-stores such as MongoLab.
When you run
$ heroku addons:add mongolab
it's provisioned for you, and there will be a config variable in the environment with the connection information:
$ heroku config | grep MONGOLAB_URI
MONGOLAB_URI => mongodb://heroku_app1234:random_password#ds029017.mongolab.com:29017/heroku_app1234
You can just read that from $_ENV['MONGOLAB_URI'], or using getenv('MONGOLAB_URI'):
$connection = new \MongoClient(getenv('MONGOLAB_URI'));
I'm trying to learn Yii, but I'm stuck with a very persistent error that rises whenever I use yiic.
First system details: Ubuntu 13.04 with apache2, php 5.5.3 and mysql 5.5.3.
I run/yiic migrate
inside /protected of the webapp I'm developing.
I get the error:
exception 'CDbException' with message 'CDbConnection failed to open the DB connection: could not find driver' in /var/www/yii/framework/db/CDbConnection.php:382
Which seems to be because at line 382 of CDbConnection.php, the script checks if pod_mysql is installed, but can't find it.
`if($this->_pdo===null)
{
if(empty($this->connectionString))
throw new CDbException('CDbConnection.connectionString cannot be empty.');
try
{
Yii::trace('Opening DB connection','system.db.CDbConnection');
$this->_pdo=$this->createPdoInstance();
$this->initConnection($this->_pdo);
$this->_active=true;
}
catch(PDOException $e)
{
if(YII_DEBUG)
{
throw new CDbException('CDbConnection failed to open the DB connection:'.
$e->getMessage(),(int)$e->getCode(),$e->errorInfo);
}
else
{
Yii::log($e->getMessage(),CLogger::LEVEL_ERROR,'exception.CDbException');
throw new CDbException('CDbConnection failed to open the DB connection.'
,(int)$e->getCode(),$e->errorInfo);
}
}
}
^^ Here is the relevant snippet of code.
The result of running
php -i|grep PDO gives
PDO
PDO support => enabled
PDO drivers => sqlite
PDO Driver for SQLite 3.x => enabled
So my problem seems to be that only the sqlite driver is running. However, I've ensured to check that php-mysql is installed (as pdo_mysql is deprecated and is inside this package). I've ran out of ideas on what to do, and will appreciate any and all help!!
Thanks in advance!
Console applications have their own config stored in protected/config/console.php. Please make sure you've got a database component configured there.
This problem is from your phpcli.ini instead of php.ini. in your phpcli.ini you should add :
extension=php_pdo_mysql.dll
as in php.ini