I have serious problem connecting Oracle (v 12c) database with php (pdo). I have IIS with PHP 7.2 version(x64). It runs .php extensions, no problem there. When i create instanse from my class i get "No Driver Found" Error. I guess my problem is not my code, but if you want to see, my code will be below my post.
I Know the problem, my problem is instantclient_12_2... I downloaded several times, i build path from Environment, it doesnt work. I remove Php 7.2 (x64) and i installed PHP 7.2 (x86), i downloaded instantclient_12_2(x32) then i build path again... But i never succeed.
And yes, i have added php_oci8.12.dll to my php.ini file, and yes, i restart my IIS several times, but it never worked! I Always get same error.
PHP Code
class PDOConnection {
private $dbh;
function __construct() {
try {
$server = "193.255.1.98"; //remote
$db_username = "SYSTEM";
$db_password = "Oracle_1";
$service_name = "ORCL";
$sid = "ORCL";
$port = 1521;
$dbtns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = $server)(PORT = $port)) (CONNECT_DATA = (SERVICE_NAME = $service_name) (SID = $sid)))";
//$this->dbh = new PDO("mysql:host=".$server.";dbname=".dbname, $db_username, $db_password);
$this->dbh = new PDO("oci:dbname=" . $dbtns . ";charset=utf8", $db_username, $db_password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
} catch (PDOException $e) {
echo $e->getMessage();
}
}
public function select($sql) {
$sql_stmt = $this->dbh->prepare($sql);
$sql_stmt->execute();
$result = $sql_stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
public function insert($sql) {
$sql_stmt = $this->dbh->prepare($sql);
try {
$result = $sql_stmt->execute();
} catch (PDOException $e) {
trigger_error('Error occured while trying to insert into the DB:' . $e->getMessage(), E_USER_ERROR);
}
if ($result) {
return $sql_stmt->rowCount();
}
}
function __destruct() {
$this->dbh = NULL;
}
}
$dbh = new PDOConnection();
$dbh->select($select_sql); $dbh->insert($insert_sql);
PDO has nothing to do with php_oci8.12.dll
. OCI8 is an entirely different extension. If you wan to use PDO with Oracle you need to use the community-driven driver. Last time I checked it was unfinished and abandoned though things might have changed. Please note you'll possibly have both in php.ini
:
;extension=oci8_12c ; Use with Oracle Database 12c Instant Client
;extension=pdo_oci
From The Underground PHP and Oracle Manual (PDF):
Oracle does not contribute to PDO_OCI.
The PHP community has let the PDO project languish and Oracle recommends using OCI8 instead whenever possible because of its better feature set, performance, reliability and stability. Use of PDO_OCI for general purpose applications is not recommended. However PDO is used by some frameworks and higher level packages, such as content management systems so you may need to use it.
There're also third-party libraries with pure PHP implementations of a PDO-compatible driver on top of OCI8 but I don't have first hand experience with any.