Oracle-Php Connection

Jyotishgher Astrology
By -
0

How to connect an Oracle database from PHP

It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.


How to connect an Oracle database from PHP

The include and require statements are identical, except upon failure:

  • require will produce a fatal error (E_COMPILE_ERROR) and stop the script
  • include will only produce a warning (E_WARNING) and the script will continue

So, if you want the execution to go on and show users the output, even if the include file is missing, use the include statement. Otherwise, in case of FrameWork, CMS, or a complex PHP application coding, always use the require statement to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.

Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.

Syntax

include 'filename';

or

require 'filename';

connection.php

<?php

//http://Localhost/cmms/dbConnection/connection.php

//$servername = "";   

$servername = "";

$username = "";

$password = "";

// Create connection

$conn = oci_connect($username, $password ,$servername);

// Check connection

if (!$conn) {

    $e = oci_error();   // For oci_connect errors do not pass a handle

    trigger_error(htmlentities($e['message']), E_USER_ERROR);

}


?>


utils/header.php

<?php

$rootPath=$_SERVER['DOCUMENT_ROOT'];

$dbFilePath="/cmms/dbConnection/connection.php";

$owner="MGR";

include_once($rootPath.$dbFilePath);

?>


Now create any Php page and call Like below


include_once($_SERVER['DOCUMENT_ROOT']."/cmms/utils/header.php");

Explanation:

  1. Include Oracle Client Libraries:

    • Ensure you have the Oracle Client libraries installed on your server. You can download them from the official Oracle website.
    • Configure your PHP environment to use the Oracle Client libraries. This usually involves adding the path to the libraries in your php.ini file.
  2. Establish Connection:

    • The oci_connect() function establishes a connection to the Oracle database.
    • It takes four arguments:
      • username: Your Oracle username.
      • password: Your Oracle password.
      • dsn: The Data Source Name. In this example, we use the following format:
        • tcp:: Indicates TCP/IP connection.
        • $host: The hostname or IP address of the Oracle server.
        • $port: The port number of the Oracle server (usually 1521).
        • $dbname: The name of the Oracle database.
  3. Error Handling:

    • The oci_error() function retrieves the error message if the connection fails.
    • trigger_error() displays the error message and terminates script execution.
  4. Close Connection:

    • The oci_close() function closes the connection to the Oracle database. This is crucial to release resources and prevent connection leaks.

Important Notes:

  • Replace placeholders: Make sure to replace the placeholders (your_username, your_password, your_host, your_database_name) with your actual Oracle credentials and database details.
  • Security: Never hardcode sensitive information like database credentials directly in your PHP code. Consider using environment variables or a secure configuration file to store these credentials.
  • Error Handling: Implement robust error handling to gracefully handle connection issues and other potential problems.
  • Data Retrieval and Manipulation: After successfully connecting to the database, you can use other Oracle Client functions (e.g., oci_parse(), oci_execute(), oci_fetch_array()) to execute SQL queries, fetch data, and perform other database operations.

This example provides a basic framework for connecting to an Oracle database using PHP. Remember to adapt it to your specific needs and requirements. The error message "SyntaxError: invalid syntax" indicates that there's an issue with the PHP code itself, not necessarily with the Oracle connection logic.


Tags:

Post a Comment

0Comments

Post a Comment (0)