I am using Zend Framework and Oracle and I am trying to receive single value from database like this:
$userId = (int) $this->_getParam('userId');
// CREATE TABLE MEMBERS (
// "MEMEBER_ID" NUMBER,
// "FIRSTNAME" VARCHAR2(20 CHAR),
// "LASTNAME" VARCHAR2(20 CHAR),
// "EMAIL" VARCHAR2(40 CHAR),
// "ABOUT" CLOB
// )
$sql = 'SELECT about FROM members WHERE id = :userId';
$binds = array (
'userId' => $userId
);
$about = Zend_Registry::get('db')->fetchOne($sql, $binds);
According to manual, fetchOne()
should return string, but when I dump returned value, it looks like this:
object(OCI-Lob)#1161 (1) {
["descriptor"] => resource(209) of type (oci8 descriptor)
}
As you can see, object is returned, which is against the manual - "it returns only a single scalar value, not an array or an object."
How can I convert this to string?
Problem is in CLOB
data type used in column about
. Answer fot this is given in notes for Oracle adapter, especially:
By default, LOB fields are returned as OCI-Lob objects. You could retrieve them as string for all requests by using driver options 'lob_as_string' or for particular request by using setLobAsString(boolean) on adapter or on statement.
So the solution is really simple, just call setLobAsString(true)
before fetchOne()
like this:
$about = Zend_Registry::get('db')->setLobAsString(true)->fetchOne($sql, $binds);
echo $about; // string