Problems with connection to ORACLE with PDO and PHP

2

A moment ago post the following question! Problems with PDO and PHP

Once the code in the connection file has been corrected, the following error appears!

  

could not find driver

     

Fatal error: Uncaught Error: Call to a member function setAttribute () on null in C: \ xampp \ htdocs \ cycle \ model \ model.gestiones.php: 19 Stack trace: # 0 C: \ xampp \ htdocs \ cycle \ view \ index.php (12): Management :: query () # 1 {main} thrown in C: \ xampp \ htdocs \ cycle \ model \ model.gestiones.php on line 19

the code I have in the model.gestiones.php file is as follows

<?php  
header("Content-Type: text/html;charset=utf-8");
class Gestiones
{
    function consulta(){
        $pdo= ConnectionDBTOAD::OpenBDTOAD();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->exec("SET CHARACTER SET utf8");
        $sql=" aqui va la consulta SQL";
        $query= $pdo->prepare($sql);
        $query->execute();
        $result=$query->fetchALL(PDO::FETCH_BOTH);
        ConnectionDBTOAD::CloseBDTOAD();
        return $result;
    }
}?>

I do not know why the error appears because the code has not been modified (this file is a copy / paste of a project that was delivered yesterday, with the difference that it was a connection to mysql.)

As I said in the previous question! I have already modified the php.ini file by removing the semicolon (;) in the line where this extension = php_pdo_oci.dll.

Thank you in advance for your answers! I'll be attentive!

PDT: this is the connection class code

<?php   class ConnectionDBTOAD{
private static $conn=null;
private static $server = "host";
private static $db_username = "user";
private static $db_password = "contraseña";
private static $service_name = "servicioX";
private static $sid   = "servicioX";
private static $port  = 1521;
private static $dbtns;

public static function OpenBDTOAD(){
    if (self::$conn==null) {
        self::$dbtns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = " . self::$server . ")(PORT = " . self::$port . ")) (CONNECT_DATA = (SERVICE_NAME = " . self::$service_name . ") (SID = " . self::$sid . ")))";
        try{
            self::$conn = new PDO("oci:dbname=" . self::$dbtns . ";charset=utf8", self::$db_username, self::$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();
        }
        return self::$conn;
    }
}
public static function CloseBDTOAD(){
    self::$conn=null;
}}?>
    
asked by accountflank 27.04.2017 в 20:28
source

1 answer

1

The problem you have is that the call to ConnectionDBTOAD::OpenBDTOAD() returns null , so $pdo is worth null and does not have a method that is called setAttribute() .

You should reinforce the code and do a type check:

<?php  
header("Content-Type: text/html;charset=utf-8");
class Gestiones
{
    function consulta(){
        $pdo= ConnectionDBTOAD::OpenBDTOAD();
        if (empty($pdo) === true) {
          die('Se produjo un error');
        }
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $pdo->exec("SET CHARACTER SET utf8");
        $sql=" aqui va la consulta SQL";
        $query= $pdo->prepare($sql);
        $query->execute();
        $result=$query->fetchALL(PDO::FETCH_BOTH);
        ConnectionDBTOAD::CloseBDTOAD();
        return $result;
    }
}

Also, I recommend you reinforce the checks you make during the execution of ConnectionDBTOAD::OpenBDTOAD() to find the root cause of the problem:

<?php
public static function OpenBDTOAD(){
    if (self::$conn==null) {
        self::$dbtns = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = " . self::$server . ")(PORT = " . self::$port . ")) (CONNECT_DATA = (SERVICE_NAME = " . self::$service_name . ") (SID = " . self::$sid . ")))";
        try{
            self::$conn = new PDO("oci:dbname=" . self::$dbtns . ";charset=utf8", self::$db_username, self::$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) {
            die('Se produjo un error grave y no se puede continuar: ' . $e->getMessage());
        }
        return self::$conn;
    }
}

By the way, that could not find driver sounds like you do not have the driver pdo_oci installed correctly. Have you tried looking at the output of a phpinfo() ? You should have a PDO that should show something like PDO drivers: dblib, oci, mysql and a separate section called pdo_oci that should have something like PDO Driver for OCI 8 and later - enabled .

If the OCI extension for PDO is not working, you should try installing the Oracle Instant Client (for example version 12.2, the last one today).

Then find the path where it was installed (possibly called "instantclient_12_2") and add it to the environment variable PATH . Restart the apache and look again at the error logs for new error or warning messages.

    
answered by 27.04.2017 в 20:36