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 $configin 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

http://www.yiiframework.com/doc-2.0/guide-db-dao.html

http://www.yiiframework.com/doc-2.0/yii-db-connection.html


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -