View Full Version : Advice on DB Abstraction Class - Conversion to MySQLi Functions (PHP5)

Velox Letum
01-10-2006, 02:20 AM
Okay, a while back I wrote a database abstraction class which allows me to use a universal set of functions while I could change however it handles it in the class. When a query was executed, it'd use the query's return reference as the key in an associative array for working easily with result sets.

This works fine with the MySQL extension, but after upgrading to PHP5 and attempting to use MySQLi (MySQL Improved extension), it returns an object. An object does not like to be used as the key in an array. Thus, I posted here for any advice you may have. Would it be possible to salvage this method? Or should I begin thinking about other ways to solve this problem?

Example function from my class:

public function sql_fetchrow($query_id = 0) {
if (!$query_id) {
// $query_id is the value of the return of a mysqli_query()
$query_id = $this->query_result;

if ($query_id) {
/* This was where I'd use the reference to fetch the row.
It retrieves the data correctly as I pass the query result outside the function.
However with MySQLi won't set $this->row[$query_id] as it returns an object now. */
$this->row[$query_id] = mysqli_fetch_assoc($query_id)
return $this->row[$query_id];
} else {
return FALSE;

ralph l mayo
01-10-2006, 02:36 AM
I think the confusion stems from mixing the procedural and OO styles.. stick with OO and (I think) you can do:

foreach ($query_id->fetch_assoc() as $key->$value)
$this->row[$key] = $value;

Sorry I can't test this right now, hope it at least partially works.

edit: assuming $this->query_result or the passed var is equal to $yourmysqliobject->query('foo'), either of which becomes $query_id

Velox Letum
01-10-2006, 02:52 AM
I could return it like that, but that kind of reduces the portability I enjoyed before. I could pass the return reference of a query to custom functions so I could manipulate the data sets stored in the rows easily. Such as to get a single field, I could pass the query reference to a fetch field function which would execute mysql_result if the results didn't exist, or if they did return $rowset[$query_id][$field]. I'm simply looking to keep my functionality. Its looking to me like I'll somehow have to pass a custom reference for the queries.

ralph l mayo
01-10-2006, 03:07 AM
What did $query_id used to be? Isn't it arbitrary? Could you just keep a counter?
$this->resultarray[++$this->query_id] = $this->query_result->fetch_array();

Velox Letum
01-10-2006, 03:20 AM
Hmm, right you are. I keep a query count anyways, so I used that. Thanks!