To connect to Oracle using Yii2, follow these steps:
- Install the OCI8 PHP extension:
Download the PHP extension from the official Oracle website (http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html).
Extract the downloaded files to a directory of your choice.
Update the "php.ini" file to include the path to the extracted Oracle files. Add the following line:
extension=oci8.so (for Linux) or extension=oci8.dll (for Windows).
Restart your PHP server.
- Configure the database connection in Yii2:
Open the Yii2 "config/db.php" file.
Replace the existing "dsn", "username", and "password" values with the following:
'dsn' => 'oci:dbname=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=YOUR SERVICE NAME)));charset=UTF8',
'username' => 'your_username',
'password' => 'your_password',
Replace "YOUR SERVICE NAME" with the actual service name of your Oracle database.
Replace "your_username" and "your_password" with the appropriate credentials.
- Test the connection:
Open the Yii2 "config/web.php" file.
Locate the "components" array.
Add the following code snippet inside the "components" array:
'db' => [
'class' => 'yiidbConnection',
'dsn' => $config['components']['db']['dsn'],
'username' => $config['components']['db']['username'],
'password' => $config['components']['db']['password'],
Save the changes and open a browser.
Navigate to your Yii2 development server.
If the connection is successful, you should see the Yii2 default welcome page.
That's it! You have successfully connected to Oracle using Yii2.