How to config Codeigniter 4 using mssql connection? - sql-server

I want to config CI4 connect using SQL Server.
if in CI3
$db['dbsqlsrv'] = array(
'dsn' => '',
'hostname' => 'localhost',
'port' => '1433',
'username' => 'sa',
'password' => 'example',
'database' => 'example',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
How config this option to work in CI4. Because when i update .env database.default.DBDriver = MySQLi to database.default.DBDriver = Mssql can't work.

to use mysql database in codeigniter 4 you should keep the For mysql you should keep the following changes.
in .env file
database.default.DBDriver = MySQLi
in app/Config/Database.php
public $default = [
...
DBDriver=>'MySQLi',
...
];

CodeIgniter 4
In app >> config >> Database.php
Check the $defaultGroup value
public $defaultGroup = 'default';
public $default = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'YOUR_USER',
'password' => 'YOUR_PASSWORD',
'database' => 'DATABASE_NAME',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'cacheOn' => false,
'cacheDir' => '',
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3308,
];

Related

What is a good practice for setting password and database user in codeigniter 4.0?

I am configuring my database in the Database.php file on codeigniter 4.0:
public $default = [
'DSN' => '',
'hostname' => 'localhost',
'username' => 'myuser',
'password' => 'mypassword',
'database' => 'mydatabase',
'DBDriver' => 'MySQLi',
'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,
];
Is it a good practice to put the username and password directly as a string or is there any other ideal method for importing these values?
In CI3 I configure it the same as you, but security is fine when you use CI syntax to handle queries with the query helper.
you can read the documentation here: https://www.codeigniter.com/user_guide/database/helpers.html?highlight=query%20helper

How to connect multiple database username randonly

Anyone can let me know how can integrate these two lines
$db_users = array('user1','user2', 'user3','user4','user5');
$db_user = $db_users[array_rand($db_users)];
with the codeigniter database connection code below to randomly connect different username?
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'mydbuser',
'password' => 'mydbpsw',
'database' => 'mydb',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

My codeigniter is using another database instead the database from database.php

I have two copies of my project, i've changed the database's name, but
even though I changed the database name in the database.php, the two projects are using the same database!!
The projects are in the same server.
One of this server the conection is like this:
$db['sas'] = array(
'dsn' => '',
'hostname' => 'MyServerIp',
'username' => 'sastest',
'password' => '************',
'database' => 'sasClone',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => FALSE);
The other is like this:
$db['sas'] = array(
'dsn' => '',
'hostname' => 'MyServerIp',
'username' => 'sas',
'password' => '******',
'database' => 'sas',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => FALSE);
and in the application i use the database like this:
$dbsas = $this->load->database("sas", TRUE);
I hope i was clear, i cannot understand why they are using the same database if the database.php is different, thank u.

https and CodeIgniter problems

Once I go to my website using https://, the CodeIgniter database loading does not work anymore. This is part of the error shown:
Message: PDO::__construct(): open_basedir restriction in effect. File(/home/students/550067/www/db/Database.sqlite) is not within the allowed path(s): (.)
This is the config file:
$db['default'] = array(
'dsn' => 'sqlite:/home/students/550067/www/db/Database.sqlite',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => 'DateBase.sqlite',
'dbdriver' => 'pdo',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
What could be the problem?

Database configuration Issue CakePHP

My database.php :
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/MySql',
'persistent' => false,
'host' => 'localhost',
'login' => '',
'password' => '',
'database' => 'cake_database',
'schema' => '',
'prefix' => '',
'encoding' => 'utf8'
);
public $test = array(
'datasource' => 'Database/MySql',
'persistent' => true,
'host' => 'localhost',
'login' => '',
'password' => '',
'database' => 'cake_database',
'prefix' => '',
//'encoding' => 'utf8',
);
}
Shows this notice : CakePHP is NOT able to connect to the database.Database connection "Mysql" is missing, or could not be created.
Please help...........
public $default = array( 'datasource' => 'Database/MySql', 'persistent' => false, 'host' => 'localhost', 'login' => 'root', 'password' => '', 'database' => 'cake_database', 'schema' => '', 'prefix' => '', 'encoding' => 'utf8' );
Mysql default login is root and password empty,
If you have change it, you must enter the user and the password to access database..
It's just because cakephp failed connect to mysql..

Resources