CodeIgniter Forums
connect to sql server with sqlsrv - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: connect to sql server with sqlsrv (/showthread.php?tid=78564)

Pages: 1 2


connect to sql server with sqlsrv - ramin.mor47 - 02-08-2021

Hi, I do not know English. I'm sorry
i set config in env
[Image: Capture.JPG]
but get error "Unable to connect to the database"

Do I have to configure it elsewhere with different specifications?

help me Huh


RE: connect to sql server with sqlsrv - captain-sensible - 02-08-2021

can you elaborate on that IP address your using for hostname; normally thats localhost and yours looks like the IP for a router or something ?

Also did you rename env to .env ?


RE: connect to sql server with sqlsrv - ramin.mor47 - 02-08-2021

(02-08-2021, 07:21 AM)captain-sensible Wrote: can you elaborate on that IP address your using  for hostname; normally thats localhost and yours looks like the IP for a router or something ?

Also did you rename env to .env ?
Yes I Chenge env to .env

Sql server in 192.168.1.4، but my web server in 192.168.1.6 , i cheng Ip 192.168.1.4 to hostname but any difference. Sad

is needed install sql server in 192.168.1.6 ? Confused


RE: connect to sql server with sqlsrv - captain-sensible - 02-09-2021

this might be relevant ? https://forum.codeigniter.com/thread-75877-post-376318.html#pid376318

you might also try using 192.168.1.4 in dsn at :
Code:
public $default = [
        'DSN'      => '',
        'hostname' => 'localhost',
        'username' => '',

which is in Database.php in app/config

i.e shift from using .env to direct in Database.php


i'm no expert on databases; in fact i shifted from main stream database eg MySQl to sqlite3 quite some time ago

could you shift to sqlite3 ? works like a dream , all you have to do is put a sqlite3 database file in writable


RE: connect to sql server with sqlsrv - ramin.mor47 - 02-09-2021

(02-09-2021, 03:08 AM)captain-sensible Wrote: this might be relevant ? https://forum.codeigniter.com/thread-75877-post-376318.html#pid376318

you might also try using 192.168.1.4 in dsn at :
Code:
public $default = [
        'DSN'      => '',
        'hostname' => 'localhost',
        'username' => '',

which is in Database.php in app/config

i.e shift from using .env to direct in Database.php


i'm no expert on databases; in fact i shifted from main stream database eg MySQl to sqlite3 quite some time ago

could you shift to sqlite3 ? works like a dream , all you have to do is put a sqlite3 database file in writable
 I can Not Chenge Database. If I do not find a solution, I have to go back to the previous version (3).


RE: connect to sql server with sqlsrv - paulbalandan - 02-09-2021

Try using 127.0.0.1 as the hostname. If that still fails, check your recent log file in the writable/logs/ folder. Check for the log record with "ERROR -->"


RE: connect to sql server with sqlsrv - ramin.mor47 - 02-11-2021

(02-09-2021, 10:16 AM)paulbalandan Wrote: Try using 127.0.0.1 as the hostname. If that still fails, check your recent log file in the writable/logs/ folder. Check for the log record with "ERROR -->"
my error log is:

Code:
ERROR - 2021-02-09 00:38:04 --> Error connecting to the database: Call to undefined function CodeIgniter\Database\SQLSRV\sqlsrv_connect()
CRITICAL - 2021-02-09 00:38:04 --> Unable to connect to the database.
#0 D:\xampp\htdocs\aradco\system\Database\BaseConnection.php(618): CodeIgniter\Database\BaseConnection->initialize()
#1 D:\xampp\htdocs\aradco\system\Database\SQLSRV\Builder.php(648): CodeIgniter\Database\BaseConnection->query('SELECT *\nFROM "...', Array, false)
#2 D:\xampp\htdocs\aradco\app\Models\Admin\Personnel\M_Personnel.php(13): CodeIgniter\Database\SQLSRV\Builder->get()
#3 D:\xampp\htdocs\aradco\app\Controllers\Admin\Personnel\Personnel.php(33): App\Models\Admin\Personnel\M_Personnel->canlogin('11578', '1')
#4 D:\xampp\htdocs\aradco\system\CodeIgniter.php(936): App\Controllers\Admin\Personnel\Personnel->Login()
#5 D:\xampp\htdocs\aradco\system\CodeIgniter.php(432): CodeIgniter\CodeIgniter->runController(Object(App\Controllers\Admin\Personnel\Personnel))
#6 D:\xampp\htdocs\aradco\system\CodeIgniter.php(333): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)
#7 D:\xampp\htdocs\aradco\public\index.php(44): CodeIgniter\CodeIgniter->run()
#8 {main}


Huh Confused


RE: connect to sql server with sqlsrv - davis.lasis - 02-11-2021

I do use multiple platforms with sqlsrv to connect to axapta database servers.
Maybe you need to use single quotes in .env file?

This is my code that does work, if this does not work for you, then most probably you have issue with connection IP address and / or DSN

.env
Code:
#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

database.default.hostname = localhost
database.default.database = intranet
database.default.username = intranet
database.default.password = intranet
database.default.DBDriver = MySQLi
database.default.DBDebug = true

# axapta server
database.axapta.hostname = 'xxx.xx.xx.xx'
database.axapta.database = 'database_name'
database.axapta.username = 'user_name'
database.axapta.password = 'long_password'
database.axapta.DBDriver = Sqlsrv
database.axapta.port = 1433

/app/Config/Database.php
PHP Code:
    /**
     * The Axapta database connection.
     *
     * @var array
     */
    public $axapta = [
        'DSN'      => '',
        'hostname' => '',
        'username' => '',
        'password' => '',
        'database' => '',
        'DBDriver' => 'Sqlsrv',
        'DBPrefix' => '',
        'pConnect' => false,
        'DBDebug'  => (ENVIRONMENT !== 'production'),
        'cacheOn'  => false,
        'cacheDir' => '',
        'charset'  => 'utf8',
        'DBCollat' => 'utf8_general_ci',
        'swapPre'  => '',
        'encrypt'  => false,
        'compress' => false,
        'strictOn' => false,
        'failover' => [],
        'port'     => 3306,
    ]; 

Controller
PHP Code:
$this->axapta Database::connect('axapta');

$builder $this->axapta->table('DATABASE_TABLE');
$builder->select();
$builder->where('TABLE_COLUMN''VALUE');
return 
$builder->get()->getResultObject(); 



RE: connect to sql server with sqlsrv - paulbalandan - 02-12-2021

Based on your error logs, it seems sqlsrv_connect() is not defined. This means that gou do not have the sqlsrv php extension installed.


RE: connect to sql server with sqlsrv - ramin.mor47 - 02-12-2021

(02-12-2021, 05:20 AM)paulbalandan Wrote: Based on your error logs, it seems sqlsrv_connect() is not defined. This means that gou do not have the sqlsrv php extension installed.
how can install?