Search code examples
phporacle-databasepdoclob

Sql update for clob field in PDO (PHP) doesn't update nor throw update execption


I've searched all over the net for examples on performing a sql update on clob fields; I believe the example I'm using is the simplest one that should work but so far none have worked. I'm trying to insert a base64 encoded image into the clob field in oracle. Below are the function and the array of clob entries. I've checked the table and verified that no update has occurred on the clob field.

// update row with new clob values
private function clobUpdate($clobArray){
    try {
        foreach ($clobArray as $item) {
            $query = "UPDATE ". static::$table ." SET ". $item["clobField"] ." = EMPTY_BLOB() WHERE ID = :ID RETURNING ". $item["clobField"] . " INTO :blob";

            $stmt = $this->db->prepare($query);
            $stmt->bindParam(':id', $item["id"]);
            $stmt->bindParam(':blob', $item["clobValue"], PDO::PARAM_LOB);
            $blob = NULL;
            $this->db->beginTransaction();
            $stmt->execute();
            $this->db->commit();
            print_r($blob);
            die();
        }
    } catch(Exception $e){
        throw new Exception($e);
    }

}


 Array
 (
    [0] => Array
    (
        [clobField] => 0
        [clobValue] => data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAMAAABrrFhUAAAAM1BMVEWxvsa7xs7CzNPM1drT2t/a4OTd4+fn6+7g5enW3eLFz9W/ydC0wcnP1924xMvJ0tjk6OzcN6J1AAAED0lEQVR4AezBgQAAAACAoP2pF6kCAAAAAAAAAAC4vbvLgV3FgQBcCYHiLzT7X+28XWkermbOSeLYMd8OqAA2Ust9u20PR0z8R44l1AYX+n5G/ot01oFPG7/I/yGd23dXn/h/yZ/MoBX+gVTxLTXzD+XQ8Rl75l+YAd+wRf6lvMO+fvKCMmDclnjJ3GHaj5cdMOzgDVKHUT3xFnOzu37PCfREek6gJ9JzAj2RrhNIvNkc9uufo2r44wMKzNj4iB+M6JnP2GDDyYckmND4mAALMp8zoF/ggyLU65NPar43ABmdbwCy+d4AZHS9AfR3Q5WPO6BZ4uNmh16DAqrZZ7CDZ3GihO77BJC7/hrgtQ4cFJH1XwFeLwEKadCpUUjQ3wX4vAVPCknQKVIKdJqUskGjTjG7/iLgsQxUijmhUaCY6LsKksl5FSRXABpR0OY9gAZ9uvcAGgWFFYA6K4BAQcV7AHEFoM4KoFBQdv4UIFcAaqwAVgArgBXACmAFsAJokaJigyqB4oLz9atKoPEVDVpkviJDicqXVOhQ+JICFTpf06HBztfsmmqg30pYSN+XQORr4gpgBbACWJfgKoOrEVqt8HoMrefwuyZfMaFF4CuC/rkRbgYJFAVXoL9rsEKPzhd0KHJQ3AFNBsUNqHJS2GlrjK6DEatjUtAcUGckikkDGoVMETlAq01/A2S/J57Q7Od9mtxwfgIkRgl1+D4DBbp1v5PUZOpAhnY7HxWgXvb0CBa/Bg84Hy7e4Hu8fAR8b4EGE4LPDSCwBRqMqAIlwOGTaA6Y0cSbQAdzxRIs6Zl322BKEzgArpqBCHOiwK9B/LRDDQZtk3epMKm6aAEFEjhgVtW/fv0JlA7XCRwwrjpfP3BqfgHo74kbzDu9BxB5RfUeQFgBmDe9B0DnAXReUmBd4yXReRtAwrrMa3bYVknXZ6BnXtVgWeFlucOugzdI3ef67SfQC2+SBgxqmbeZO6zpJ29VOkypkzebFXa0xAfkBhtaFBgmrFeNfFCsUK3XzIfl2qHVOCcFzHNAoz1STNyhzAiZonIY0GMvfEHZnX58XdtgL3xV2Z1+fA3bYC9Uouy+Pr6CbdAK1SkNQvovU6X863jeOCbVmscQ3vvOTkJNNCBVPKNmGpGrs+ULRFAzjcnVyfIFImiJRqWG60ahYWXgot+kafOHK0akeXHo//xKN0E/+BGlax+RqXEE5zb5IXOTXb/9BLZJek5gTComMIIk8YOS2PADrYLiUfG6BtIf/KjD+QYgh8UbQP5PCSY/awpMRdVtt34FCoxl7vy07vsEkLv8CbDWCmR+Wpbvgoz1QpUfV+VHQdpqBiM/Lpq+AwVuQX6efB9oqhds/Ly2AlgBrAD+3XB+Ca4yiMSPS/hv/wEp/tUncVv5oQAAAABJRU5ErkJggg==
        [id] => 25
    )

)

***** update

Per Fred's error handling link and this 2007 patch I'm now able to update my clob. Updated function below:

// update row with new clob values
private function clobUpdate($clobArray){
    $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    try {
        foreach ($clobArray as $item) {
            $query = "UPDATE ". static::$table ." SET ". $item["clobField"] ." = :CLOB WHERE ID = :ID";
            $stmt = $this->db->prepare($query);
            $stmt->bindParam(':ID', $item["id"]);
            $stmt->bindParam(':CLOB', $item["clobValue"], PDO::PARAM_STR, strlen($item["clobValue"]));
            $blob = NULL;
            $this->db->beginTransaction();
            $stmt->execute();
            $this->db->commit();
            print_r($blob);
            die();
        }
    } catch(PDOException $e){
        echo "Exception ". $e->getMessage();
        throw new Exception($e);
    }
}

Solution

  • NOTA: Posting as a community wiki to mark the question as solved.

    You are using :ID and :id. Those are case-sensitive.

    WHERE ID = :ID ... bindParam(':id', => bindParam(':ID',

    Yet, from the link you found about a patch, seems to also have contributed to successfully updating your database.

    As per your comment:

    "Per Fred's error handling link and this 2007 patch I'm now able to update my clob. Updated function below:"

    // update row with new clob values
    private function clobUpdate($clobArray){
        $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        try {
            foreach ($clobArray as $item) {
                $query = "UPDATE ". static::$table ." SET ". $item["clobField"] ." = :CLOB WHERE ID = :ID";
                $stmt = $this->db->prepare($query);
                $stmt->bindParam(':ID', $item["id"]);
                $stmt->bindParam(':CLOB', $item["clobValue"], PDO::PARAM_STR, strlen($item["clobValue"]));
                $blob = NULL;
                $this->db->beginTransaction();
                $stmt->execute();
                $this->db->commit();
                print_r($blob);
                die();
            }
        } catch(PDOException $e){
            echo "Exception ". $e->getMessage();
            throw new Exception($e);
        }
    }