How to get the contents of an Oracle CLOB data field in PHP

The Oracle “CLOB” (Character Large Object) is a data type used to store up to 4 Gigabytes of text. Retrieving the contents of a CLOB is not as intuitive as you might think.

Let’s say you have a CLOB field/column named “mychars” in an Oracle DB table named “mytable” along with some other fields. You want to simply echo out the text in the “mychars” field:

<?php
    $id = '3';
    $conn = oci_connect('myusr', 'mypass', 'mydb');
    if (!$conn){
        echo 'Connection error.';
    }
    $sql = 'SELECT * FROM mytable WHERE myid=:id';
    $stid = oci_parse($conn, $sql);
    oci_bind_by_name($stid, ":id", $id);
    $result = oci_execute($stid);
    if($result !== false){
        while($row = oci_fetch_assoc($stid)){
            echo $row['mychars'];
        }
    }
?>

The above code will give you an error that looks like the following:

Catchable fatal error: Object of class OCI-Lob could not be converted to string in somefile.php on line 14

If you try to do a print_r() on the CLOB in an attempt to figure out what you are dealing with you will get something that looks like:

OCI-Lob Object ( [descriptor] => Resource id #3 )

This is because a Lob object is returned instead of the contents of the CLOB.

To get the CLOB contents you will need to call the load() or read() methods on the returned object. The latter will require the length of data to read in bytes but has the advantage of not being limited by the script memory limit:

<?php
    $id = '24382';
    $conn = oci_connect('myusr', 'mypass', 'mydb');
    if (!$conn){
        echo 'Connection error.';
    }
    $sql = 'SELECT * FROM mytable WHERE myid=:id';
    $stid = oci_parse($conn, $sql);
    oci_bind_by_name($stid, ":id", $id);
    $result = oci_execute($stid);
    if($result !== false){
        while($row = oci_fetch_assoc($stid)){
            echo $row['mychars']->load();
            //or
            echo $row['mychars']->read(2000);
        }
    }
?>

How to reverse the keyhole on a Kwikset Lever so the teeth face up

So let’s say you just purchased a Kwikset entry lever from Lowes. You manage to get it installed and even get the levers in correct orientation. If you are lucky the keyhole will be correctly oriented so you can insert the key with the teeth up. If you are not so lucky, the keyhole will be upside-down so the key goes in with the teeth down.

Allegedly the lock will still work even if the tumbler springs have failed, if the keyhole/cylinder is oriented so the teeth go in up so this is the desirable orientation. The directions included with the entry lever do not tell you how to do this.

Fortunately, Kwikset has a video on their website that shows how to reverse the cylinder. There is a small clip on the cylinder you can push/pry out with a small flat-head screwdriver. You then pull the cylinder straight out, rotate it 180 and then push it back in and re-install the clip.