Call to undefined function oci_connect()
When you encounter a Fatal error: Call to undefined function oci_connect() in your PHP environment, it is almost always an extension loading issue rather than a syntax error in your script. The code provided shows a standard Oracle connection attempt using the legacy OCI8 driver:
$conn = oci_connect('localhost', 'username', 'password') or die(oci_error());
The fact that oci_error() is also undefined reinforces the diagnosis: the OCI8 extension is simply not available to the PHP interpreter running your script. This typically happens on shared hosting or local development setups like XAMPP where extensions are either missing from the php.ini configuration or not compiled into the binary.
Verifying Extension Availability
Before modifying configuration files, you must confirm whether the extension exists in your environment but is merely disabled. Run the following command in your terminal or CLI:
php -m | grep oci
If this returns nothing, the extension is missing entirely from the loaded modules. If you see oci8 listed but still get the error, check your php.ini file. The line might be commented out with a semicolon:
;extension=oci8.dll
Uncomment it to enable the driver. However, simply enabling the line is insufficient if the corresponding DLL file does not exist in your ext directory.
Locating and Enabling the DLL
The source material mentions checking the PHP DLL files in the ext folder. This is a critical step. On Windows environments like XAMPP, the extension usually resides at a path similar to:
C:\xampp\php\ext\php_oci8_19a.dll
(Note: The exact filename depends on your PHP version and Oracle client version.)
If this file is missing, you cannot simply "enable" it. You have two distinct paths forward depending on your hosting situation.
On local development machines, the most common cause of a missing DLL is that the Oracle Instant Client was not installed correctly or the php_oci8.dll was not copied to the ext directory. You must download the matching Oracle Instant Client, extract it, and ensure the compiled extension matches your PHP architecture (32-bit vs 64-bit). Mismatched architectures will result in a silent failure where the extension never loads.
On shared hosting, you generally cannot install system libraries or copy DLL files manually. In this scenario, the error indicates that the host server does not support Oracle connections at all. Attempting to use oci_connect() here will fail regardless of your code logic. You must either switch to a different database (MySQL/PostgreSQL) using PDO or SQLi, or request from your hosting provider that they enable the OCI8 extension if they support Oracle databases.
Modern Alternatives and Driver Selection
While oci_connect is functional, it belongs to the procedural style of PHP which has largely been superseded by Object-Oriented approaches. If you are building a new application or refactoring legacy code, consider using PDO with the OCI driver (pdo_oci). This provides a more robust connection interface and better error handling:
$dsn = 'oci:host=localhost;user=username;password=password';
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
];
try {
$pdo = new PDO($dsn, 'username', 'password', $options);
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
The trade-off here is that pdo_oci requires a slightly different setup; specifically, you need to ensure the pdo_oci.dll exists in your extensions folder. If you are on Windows and cannot find the DLL, it is often easier to install the Oracle Instant Client and then run a build script to compile the extension against your specific PHP installation.
Troubleshooting Checklist
If you have verified the file locations and enabled the extension but still see the error, check your php.ini for conflicting settings or incorrect paths. Sometimes, multiple php.ini files exist (e.g., one for CLI and one for Apache), causing the web server to ignore the configuration changes you made in the CLI.
Finally, ensure that your PHP version matches the Oracle Instant Client version. Using a client designed for PHP 8.x with a DLL compiled for PHP 7.x will result in the function remaining undefined. Always verify compatibility before deploying production code against an Oracle database.
