@deron
Yii is a PHP framework that provides various components for developing web applications. One of the important components in Yii is the database connection, which is used to connect to a database and execute SQL queries. Here are the steps to configure a database connection in Yii:
1 2 3 4 5 6 7 8 9 10 |
'components' => [ 'db' => [ 'class' => 'yiidbConnection', 'dsn' => 'mysql:host=localhost;dbname=mydatabase', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ], // other components ], |
The dsn
parameter specifies the Data Source Name for the database connection. In this example, we are connecting to a MySQL database running on the local machine, with a database name of mydatabase
. The username
and password
parameters are used to authenticate with the database server.
1 2 3 |
$connection = Yii::$app->db; $command = $connection->createCommand('SELECT * FROM mytable'); $rows = $command->queryAll(); |
This code retrieves all the rows from a table called mytable
and stores them in the $rows
variable.
By following these steps, you should be able to configure and use a database connection in Yii.