Connect Laravel to SQL Server - sql-server

Hi I have been trying to connect for two days actually I need to connect to SQL SERVER from my Laravel project. I have installed all the required drivers
extension=php_pdo_sqlsrv_80_ts_x64.dll
extension=php_sqlsrv_80_ts_x64.dll
and I added them in the folder ext of php 8.0.25
I also installed msodbcsql.msi (Microsoft ODBC Driver for SQL Server)
But I still get this error...???
POST http://localhost/api/login 500 (Internal Server Error)
I connect in Windows Authentication mode to the sql server
will I need to provide a DB_USERNAME ?
.env file
DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1
DB_PORT=1433
DB_DATABASE=ecommerce
DB_USERNAME=
DB_PASSWORD=
database.php
'sqlsrv' => [
'driver' => 'sqlsrv',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
],

For those who come after
my problem was in .env file
Solution
with : windows authentification
DB_CONNECTION=sqlsrv
DB_HOST=your_server_name ### example: xxx\xxx
DB_PORT=null
DB_DATABASE=ecommerce
DB_USERNAME=
DB_PASSWORD=
with : sql server authentification
DB_CONNECTION=sqlsrv
DB_HOST=your_server_name ### example: xxx\xxx
DB_PORT=null
DB_DATABASE=ecommerce
DB_USERNAME=sa
DB_PASSWORD=your_password

Related

'Invalid DB driver' error when connecting CodeIgniter framework to Sql Server

This is the error:
An Error Was Encountered.
Invalid DB driver
I am using CodeIgniter 3.1.9 framework for a website and I am on the process of connecting the database I imported on HeidiSQL to the framework.
To connect to the database, I decided to install sql server drivers for the specific php version I have (PHP 7.2.9). By the way, I have Microsoft SQL Server 2012 Native Client installed and my operative system is a Windows 7 Professional 64 bits.
I created a folder inside CodeIgniter named 'sqlsrv' and installed the drivers inside.
I configured php.ini file like this, adding the extensions needed:
extension=sqlsrv
extension=php_sqlsrv.dll
extension=php_sqlsrv_72_ts_x64.dll
extension=php_sqlsrv_72_ts_x86.dll
extension=php_pdo_sqlsrv_72_ts_x64.dll
extension=php_pdo_sqlsrv_72_ts_x86.dll
extension=php_pdo.dll
And here is the database.php configuration file (server/hostname, port, username, database/dbname and password values are not real for safety):
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => 'Driver={SQL Server 2012 Native Client};server=192.168.7.88;port=1422;dbname=database_name;username=user_name;password=pass',
'hostname' => '192.168.7.88',
'username' => 'user_name',
'password' => 'pass',
'database' => 'database_name',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE ,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
I tried changing the content inside 'dsn' value and leaving it empty, enabling more sql server drivers and infinite other ways like using odb (not useful because query builder is not allowed) alternatively and it's still not working.

How to I specify Tinker to use a different database connection?

I have two database connections. One for my application and another for testing.
In my ..\config\database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'testing' => [
'driver' => 'mysql',
'host' => env('DB_TEST_HOST', 'localhost'),
'database' => env('DB_TEST_DATABASE', 'forge'),
'username' => env('DB_TEST_USERNAME', 'forge'),
'password' => env('DB_TEST_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I am able to change the database connection in seeding using
php artisan db:seed --database=testing
I wanted to use tinker for the connection 'testing' but unable to change it. Is there any way to change the database connection for tinker similar with database seeding?
As your question starts with using one database for testing/development and one for production, you should look into using different environments, this will allow you to have no change in your code between deployment & local testing.
This task can easily be achieved by specifying your environment:
php artisan tinker --env=local
By default, if you specify no --env, you will be using /your-app/.env
When using local you read variables from /your-app/.env.local
For your specific use case:
php artisan db:seed --env=local
Further reading for Laravel 5.1: https://laravel.com/docs/5.1/configuration
Latest version: https://laravel.com/docs/configuration
NB: You should avoid checking in the ".env" file to VCS, the .env.local should be OK to share, but it is best practice to not bundle production credentials with your VCS.
To set the default database connection to 'mysql_test' from within tinker I use this command:
>>> use DB
>>> DB::setDefaultConnection('mysql_test');
It is especially useful when you want to test your migrations and seeders without messing up your existing (working) local database.
Change default connection
$model_instance = new App\YourModel();
$model_instance->setConnection('new_connection');
$data = $model_instance->find(1);

Laravel connection to sqlserv

I'm getting error:
"SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)"
when I try to get answer from server by
$users = DB::connection('sqlsrv')->select('Select quotation_username from users');
and yes - I have set:
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST2', 'localhost'),
'port' => env('DB_PORT2', '1433'),
'database' => env('DB_DATABASE2', 'forge'),
'username' => env('DB_USERNAME2', 'forge'),
'password' => env('DB_PASSWORD2', ''),
'charset' => 'utf8',
'prefix' => '',
],
Any feedback would be appreciated :)
EDIT:
I tried "php arsian serve" as suggested and got an error message:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/20160303/sqlsrv' - /usr/lib/php/20160303/sqlsrv: cannot open shared object file: No such file or directory in Unknown on line 0
.env content:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=inventoryapp
DB_USERNAME=root
DB_PASSWORD=****
DB_CONNECTION2=sqlsrv
DB_HOST2=****
DB_PORT2=1433
DB_DATABASE2=i4
DB_USERNAME2=****
DB_PASSWORD2=****
There is file called .env in the root folder, set the DB_HOST2 and DB_PORT2 value there.
after that re-run the php artisan serve

Cakephp 3 Bake all issue with MSSQLSRV

I am using Cakephp 3 and MSSQLSRV 2014. I made all the necessary changes to connect to MSSQL server. In GUI, I can see that cakephp can connect to MSSQL. Please see the below screenshot.
So now when I go to bin directory to bake the application, I get below error unable to load MSSQL driver, which is already installed:
Here are my datasource settings in app.php file:
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Sqlserver',
'persistent' => false,
'host' => 'localhost\SQLEXPRESS',
/**
* CakePHP will use the default DB port based on the driver selected
* MySQL on MAMP uses port 8889, MAMP users will want to uncomment
* the following line and set the port accordingly
*/
'port' => '1433',
'username' => 'sa',
'password' => 'password',
'database' => 'ServerMatrix',
'encoding' => 65001,
'timezone' => 'UTC',
'cacheMetadata' => true,
'log' => false,
I am thinking its probably a bug that should be reported to CakePHP community, but wanted to get some help from stack-overflow community to see if they have encountered such issue.
In command line interface typed php --ini and opened loaded php.ini file and added SQLSrv extensions. That solved the problem for baking an application.
Thanks to #ndm.

cakephp error. Connection to database ...not ...: Access denied for user 'my_app'#'localhost' (using password: YES)

I am trying to start CakePHP. I made bookmarker and tested by command bin\cake server. It showed one error as connection to database could not be established:
SQLSTATE[HY000] [1045] Access denied for user 'my_app'#'localhost' (using password: YES).
I read the config/app.default.php file. It says there is a database my_app and another database test_myapp with some users. I can not find these databases in phymyadmin in xampp. Am I supposed to create the named databases and users manually? I thought CakePHP should create these automatically. Or should I give names of databases etc. which I like and create the same.I'm using xampp with windows 7 and am very new to CakePHP.
Cake will not create the database or database user for you. You need to create them yourself and then match these database credentials into the db config file. Your datasource in app/Config/app.php file should look something similar to:
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'my_db_user',
'password' => 'my_db_user_password',
'database' => 'cake_database_name',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
]
],
In this example you would have to create a database named: cake_database_name, and then add a database user named my_db_user with the password of my_db_user_password.
Edit Config/app_local.php file instead of Config/app.php with your database connection parameters, i.e. host, username, password, database name. This will solve the problem.
Be sure to create same database first and providing enough privileges to same user you're writing in app_local.php file.
See in Mysql.php:
class Mysql extends Driver
{
use MysqlDialectTrait;
use PDODriverTrait;
/**
* Base configuration settings for MySQL driver
*
* #var array
*/
protected $_baseConfig = [
'persistent' => true,
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'cake',
'port' => '3306',
'flags' => [],
'encoding' => 'utf8',
'timezone' => null,
'init' => [],
];
Then in app.php write
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
/**
* CakePHP will use the default DB port based on the driver selected
* MySQL on MAMP uses port 8889, MAMP users will want to uncomment
* the following line and set the port accordingly
*/
//'port' => 'non_standard_port_number',
'username' => 'root',
'password' => '',
'database' => 'cake',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
...],
In phpMyAdmin
create cake database.
It's all.
Edit Config/app_local.php file with your database connection parameters. You will find host, username, password, database name their.
don't edit config/app.php with database connection parameters.
Your problem will be now solved
CakePHP 4.x
Error:
SQLSTATE[HY000] [1045] Access denied for user 'my_app'#'localhost'
(using password: YES).
Reason:
No user id: "my_app" with password: "secret" [or any other] set in config/app.php
file.
Solution:
In case of WAMP, go to phpmyadmin page, then we can select cake_cms
database -> privilage-> create user id and password, same as in config
file config/app.php, refresh the http://localhost:8765/ page.
it should now show
CakePHP is able to connect to the database.
If you are using wamp, make sure port is uncomment in Datasources and 'port' => '3308'
I solved this problem by checking port at phpmyadmin top
and uncommit below line in app_local.php
'port' => '3308',
CakePHP3.9.3,app_local.php,'username`=>'root','password'=>'','database' => 'db name'

Resources