HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

MySQLi_Recordset: blending SPL and Statement/Query results

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
splstatementblendingquerymysqli_recordsetandresults

Problem

I've made a SPL-based class named "Recordset" that wraps both MySQLi_STMT and MySQLi_Result objects and allows treating either as a 3-dimensional array. It requires PHP5.3+.

I'm pretty bummed about the slow foreach loop over my Recordset object, by the slower internal seek and fetch speeds within Recordset, and by the fact that nothing can even compare to the old while ($MySQLI_STMT->fetch()) {}. I've already cached certain result information, such as num_rows and class name, and try to avoid chaining internal function calls, which speeds things up considerably. Unfortunately, this cuts down on proper internal data validation checks. The constant data_seek calls do slow things a little. I'd like for this to work better. Any suggestions or insight how to speed up foreach loops and internal row fetching?

Here's excerpts of the important parts...

```
class Recordset implements Iterator, ArrayAccess, Countable {

private $MySQLi_set; // MySQLi_STMT or MySQLi_Result object
private $MySQLi_set_type; // cache of MySQLi_STMT or MySQLi_Result object type to avoid repeat instanceof calls
private $field_metadata; // array of field info objects returned by query
private $num_rows; // cache row count to avoid repeat mysqli_x->num_rows call
private $bind_row; // binding array for MySQLi_STMT results
private $pointer; // for tracking iterations over records in query

function __construct($sql) {
$bind_params = func_get_args();
array_shift($bind_params);
try {
if (count($bind_params) === 0) {
// expecting MYSQLI_Result obj returned, replace code as necessary
$this->MySQLi_set = DB_Main::query($sql);
}
else {
// expecting MySQLi_STMT obj returned, replace code as necessary
$this->MySQLi_set = DB_Main::statement($sql, $bind_params);
}
}
catch (Exception $e) {
throw new

Solution

bob-the-destroyer withdrew the question in a comment above:


taking from a comment on PHP's man page on Mysqli classes, apparently
you can extend them. Who knew? So, I've scrapped this idea and
extended Mysqli_stmt to auto-handle param and result binding on
construct. So I now have a simple old-school query-like api for
parametrized statements with a simple fetch_assoc method. Array
interface doesn't seem so important anymore.

(just posting this so this question doesn't show up as "unaswered" any more

Context

StackExchange Code Review Q#401, answer score: 3

Revisions (0)

No revisions yet.