Database configuration Issue CakePHP - 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..

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 config Codeigniter 4 using mssql connection?

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,
];

Missing Datasource Configuration when run cakephp project in live

Showing error when run project in live server.error as been below
Missing Datasource Configuration Error:
The datasource configuration
default was not found in database.php.
my database.php file
class DATABASE_CONFIG {
public $default;
function __construct(){
$this->default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'xxxxxx',
'database' => 'demo',
'prefix' => '',
);
}
}
please help me to resolve that issue. Thanks in advance
Please use below code :
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'your_db_name',
'prefix' => '',
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'test_database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
}
change in first db configuration only do not change in $test configuration. Leave as it is.
I got a default equal to this, see if it helps
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'tutorial',
'prefix' => '',
//'encoding' => 'utf8',
);

CakePHP 3 Multiple Databases

I want to connect to a second (remote) database using CakePHP 3. I have found solutions online that suggest how to associate different models with different databases but that is not what I need to achieve.
I need to be able to connect to a remote database (that is not associated with any model) and read/write some records from an action in my controller. Can this be achieved using CakePHP?
Edit (more information):
I have a website that acts as a booking platform for hotel rooms. The availability of these rooms can be controlled via my website and stored in my database. But for some clients I want to be able to connect to their private database directly and use their records to check availability.
Follow the Below Instructions
Step 1 : Open config/app.php Find Datasources array and add Remote Database configuration like as -
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'port' => '3306',
'username' => 'YOUR_DB_USER',
'password' => 'YOUR_DB_PASS',
'database' => 'YOUR_DB_NAME',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => false,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
],
'remote_db_1' => [ /*Remote Database 1*/
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '192.168.1.47', /*YOUR_REMOTE_SERVER_IP*/
'port' => '3306',
'username' => 'REMOTE_DB_USER',
'password' => 'REMOTE_DB_PASS',
'database' => 'REMOTE_DB_NAME',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => false,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
],
'remote_db_2' => [ /*Remote Database 2*/
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => '192.168.1.47', /*YOUR_REMOTE_SERVER_IP*/
'port' => '3306',
'username' => 'REMOTE_DB_USER',
'password' => 'REMOTE_DB_PASS',
'database' => 'REMOTE_DB_NAME',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => false,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
],
Step 2 : Now you can access Remote database in your controller like as-
use Cake\Datasource\ConnectionManager;
use \PDO;
class YourController extends AppController{
public function getRemoteData(){
$conn1 = ConnectionManager::get('remote_db_1'); #Remote Database 1
$conn2 = ConnectionManager::get('remote_db_2'); #Remote Database 2
}
}
Note: Now you can use PDO methods to Insert,Retrieve,Update
Example :
class YourController extends AppController{
public function getRemoteData(){
$conn1 = ConnectionManager::get('remote_db_1'); #Remote Database 1
$sql = "SELECT * FROM users";
$query = $conn1->prepare($sql);
$query->execute();
$result = $query->fetchAll(); #Here is the result
}
}

Cakephp3 multiple databases

I wanted to ask, if is possible to work with 2 databases in cakephp3, using raw sql ?
I have query like this:
select * from shop.brochures b, upload.documents up where b.doc_id = up.id;
The problem is, this are 2 databases.
I don't know, how setup connection, i think that will not work ( doc example )
$conn = ConnectionManager::get('default');
Thank You for any advice.
in /config/app.php you can set as many db as you want; i have 2.
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'sisarticulos',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
],
'gente' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'sggeneral',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,ยด
then in the controller you can set
$conn = ConnectionManager::get('default');
//some code
$conn = ConnectionManager::get('get');
use default first and then use gente

Resources