Laravel 9 (sqlsrv) could not find driver - php

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

Related

Error in migration of table in Laravel, though there is no problem in connecting with database

When I am trying to migrate a table in laravel with the command php artisan migrate --path="2022_04_17_161039_create_cruds_table.php", I am getting this error -
Illuminate\Database\QueryException
could not find driver (SQL: select * from information_schema.tables where table_schema = crud and table_name = migrations and table_type = 'BASE TABLE')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:745
741▕ // If an exception occurs when attempting to run a query, we'll format the error
742▕ // message to include the bindings with SQL, which will make this exception a
743▕ // lot more helpful to the developer instead of just the database's errors.
744▕ catch (Exception $e) {
➜ 745▕ throw new QueryException(
746▕ $query, $this->prepareBindings($bindings), $e
747▕ );
748▕ }
749▕ }
+33 vendor frames
34 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
But when I am trying to connect with the database with php artisan db mysql, it's working totally fine.
Please help me, if there is no problem in connecting with the database, then why migration command is not working.
Also, I have enabled the pdo_mysql driver.
PHP needs to php-mysql extension to communicate with MySQL, make sure to install and have this extension in your system, if you installed it before and you have more than one PHP version, make sure to have this extension for your needed PHP version

Class 'PDO' not found Error in Upload Laravel project on server [duplicate]

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

Laravel SQLSTATE[HY000] [1049] Unknown database 'previous_db_name'

I have this error when i use php artisan migrate in my Laravel project.
[PDOException]
SQLSTATE[HY000] [1049] Unknown database 'previous_db_name'
this is my database.php file :
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'current_db_name'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
I saw this error in this question and this question but none of them was not helpful.
It clearly mentions that there's no such database named previous_db_name.
From what it seems the database.php file is not where the variable is from.
Check the .env file in your Laravel installation folder to see if that's the database name that you have wrongly specified.
Please run this command:
php artisan config:cache
php artisan cache:clear
AND
restart your server
I had to kill my server and re-run php artisan serve for my project to register the change of my DB_DATABASE name. I kept getting the error even after clearing the cache because I didn't realize this.
In Laravel 5.x You should define the DB Details in two files
.env file in the project folder
.database.php file inside config folder
If you are using PHP artisan serve just restart it. (just in case someone comes searching as I did).
set your database name in .env file too.
I know, it is super late but for people like me, new in laravel and following tutorial from artisansweb. Note that, migrate will not create your database. Rather, it will just create all the tables. Assuming, you have set up your .env file. Now the important part is create the database and users (if you decided to go with custom users) manually. Then, do the php artisan migrate command
hope that helps.
Try this :
<?php
class Config {
private $host = "localhost";
private $db_name = "db_ahp";
private $username = "root";
private $password = "";
public $conn;
public function getConnection() {
$this->conn = null;
try {
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
} catch (PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
This might be late but I am just updating in case someone else faces a similar issue. I came across the same error, in my case it happened to be due to a missing database in xampp. i.e I was literally running a migration on an inexisting database in xampp.
Kill your server in case it's running. (Use Ctrl+C on windows.)
Make sure that you created the actual DB in your phpMyAdmin.
Run the following command to clear the cache: php artisan cache:clear
Run php artisan migrate once again.
Everything should be good after this.
Happy coding.

How do I host a webapp using php and mongodb on heroku?

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'));

CDbConnection failed to open the DB connection Yii

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.

Categories