How to connect to oracle using yii2?

by elise_daugherty , in category: PHP General , 5 months ago

How to connect to oracle using yii2?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jerad , 5 months ago

@elise_daugherty 

To connect to Oracle using Yii2, follow these steps:

  1. 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.
  2. 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.
  3. 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.