How to connect to a database without the config file in Yii2? -
i using, same problem, code given in question yii2 create database connection.
i realise $config
variable no longer 1 in web.php file, "config" folder, , changing $config
in configuration::setconfig()
function.
my question more experienced me in yii should write
- in web.php file in
db
field (or in db.php file) "create database connection programmatically without using config file" ? - in function
configuration::setconfig()
configure application?
i'm sorry if question not clear enough. please ask details in comments if needed. thank you!
you can define new connection way
$db = new yii\db\connection([ 'dsn' => 'mysql:host=localhost;dbname=example', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ]); , $db->open();
after db connection established, 1 can execute sql statements following eg: :
$command = $db->createcommand('select * post'); $posts = $command->queryall(); or $command = $connection->createcommand('update post set status=1'); $command->execute();
you can @ doc , guide
Comments
Post a Comment