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:


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:

load();
            //or
            echo $row['mychars']->read(2000);
        }
    }
?>

Comments

11 responses to “How to get the contents of an Oracle CLOB data field in PHP”

  1. jason Avatar

    Another point is that if your data from the database is null then php will set it as a string otherwise the data will be an object.

    Just make sure your doing is_object before you use ->load or ->read.

  2. erik Avatar
    erik

    Excellent, exactly what I was looking for. Thanks!

  3. Kunal Avatar
    Kunal

    Thanks alot, this helped me finish my page!

  4. Akay Avatar
    Akay

    I think I browsed around 100 different sites for the solution. Finally I got the solution through your site. Thank you

  5. Rita Avatar

    Hello everyone,

    I am also facing the exact problem. And I did the same above. But still nothing displays on the page. Please do help me.

    Can I call directly print $a->load();
    Or do I need to write any function for load()?? Please please do suggest me. I am stucked in there.

  6. Furmans Avatar
    Furmans

    thank you very much !
    best regards!

  7. Jair Avatar
    Jair

    Thank you!

  8. Luis Avatar
    Luis

    Thank you very much!!

  9. alifaiq Avatar

    when empty record selected, it will be “Call to a member function load() on null “

  10. Bob Avatar
    Bob

    Doesn’t seem to work if your LOB object is a ROWID type 🙁

  11. haluk ünal Avatar
    haluk ünal

    hello from 2018, thanks for your explanation. it was lifesaver

Leave a Reply