php - pdo_oci driver not found but is listed in available drivers
I am trying to connect to an Oracle Database 11g XE running on windows from a remote Ubuntu 14.04 server using PDO_OCI in php. I followed this guide http://gist.github.com/tassoevan/10392954 (numerous times) and keep getting the following error:
could not find driver
However when I call PDO::getAvailableDrivers() it gives me this:
Array ( [0] => mysql [1] => oci [2] => sqlite )
Note: oci wasn't there before I followed the guide in the link above.
All of the other tutorials I could find were basically the same thing.
I have the Oracle instant client installed (version 11.1). The latest version was not even getting the driver to show up in the list of available drivers.
Also I know that the driver which is used in this method is older but there are no step by step guides on how to get it working with the lastest one. I am new to Oracle Databases as well as php so step by step help would be greatly appreciated.
UPDATE: I figured out the driver problem. It was in my php code, and something very silly. I am posting for the benefit of others.
My original code had "OCI" in capital letters when setting the connection.
$tns = "
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = myIP)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = XE)
)
)
";
try{
$pdo = new PDO('OCI:dbname='.$tns ,'username','password'); // Capital 'OCI' here is WRONG so changed to 'oci:dbname='
} catch(PDOException $e){
echo $e->getMessage();
}
print_r(PDO::getAvailableDrivers());
However, I am having problems querying the Database. It seems nothing is happening. I have allowed the port 1521 in the Windows firewall for both incoming and outgoing. Any suggestion?
Here is my php code:
$stmt = $pdo->prepare("SELECT * FROM `PDO`;");
$stmt->execute();
$result = $stmt->fetchColumn(0);
echo $result;
The tablePDO
only has one column with one row.
Does the Oracle database need to be configured to allow for remote connections?
Answer
Solution:
There were two problems with my PHP code. 1) The way the PDO connection was initiated was incorrect ... see update in original post. That solved my driver problem.
2) Oracle doesn't seem to like the backtick character, unlike MySQL. So I had to remove them from my query to look like this
This solved my database querying problem.