PEAR DB also supports a handful of portability options which can be used to ensure seamless portability between different database types. For instance, DB_PORTABILITY_NULL_TO_EMPTY will convert the NULL values that Oracle outputs for empty strings back into empty strings. There are a number of other compatibility settings, all of which are enabled using the DB_PORTABILITY_ALL flag. Lastly, the seqname_format option can be used to specify a sprintf() formatting routine to be used on sequences. Sequences are used in PEAR DB to allow the retrieval of the next available ID using nextID(). This is particularly useful when preparing multiple related INSERT statements. To pass any combination of options to the connection, the following method is used:
$options = array(
'debug' => 2,
'portability' => DB_PORTABILITY_ALL,
);
With the connection established without error, a query can be passed to the database object using the query() function, which accepts a string comprised of a valid SQL statement.
$res =& $db->query('SELECT * FROM enrolments');if (PEAR::isError($res)) {
die($res->getMessage());
}
To make query construction easier, question marks can be used as placeholders. These placeholders can be populated at runtime by passing the query function an array of values to be substituted. For instance, the following example passes id values to be used in the WHERE clause of the embedded SQL statement.
$sql = 'select * from enrolments where
studentId = ? and subjectId = ?';
$data = array(1, 2);
$res =& $db->query($sql, $data);