help needed with simple lightweigh Db wrapper class (mysql functions)

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am refactoring some code that is liberally sprinled with mysql_*
statements. I am changing the code to remove all the mysql_* procdural
calls with calls to a database wrapper - in order to abstract the db
implemntation away from the 'business logic'

I initially intend to implement the db implementation using mysqli (OO)
methods. I have discovered though that the mapping between the function
calls is not exactly straightforward.

The functions I most need to replace/refactor are:


I need to replace (i.e. mimic the behaviour of these two functions) but
there appear to be "issues".

mysqli_query() returns a result object, which must be freed before if
can be used again ( ). This
means that using my wrapper class, every time I invoked the method that
called mysqli_query(), I would have to manually close the returned
result - this is error prone, and I would have liked the class to deal
with releasing resources internally.

For example (pseudocode) if my wrapper method was:

function MyDbClass::fetchTheData($sql){$result = mysqli_query($sql);}

then using it my code would look something like this:

//PHP script
$my_result = $dbwrapper->fetchTheData($sql);

//IIUC - I cant run any other queries with the database until I call the
close method on $my_result (is my understanding correct BTW?).

The only way to get around this is to CLONE the data returned by the
fetchTheData() method (disaster for large data sets), close the result
set - which is a private member variable in the DbWrapper class, and
then return the cloned result - am I barking up the wrong tree?

I'd appreciate help in implemnting the functionality provided by the two
functions mysql_query() and mysql_result(), by using mysqli:: methods.
The online docs etc are not helping much so far.

Re: help needed with simple lightweigh Db wrapper class (mysql functions)

Ronald Raygun wrote:
Quoted text here. Click to load it

No, you should manually close the result when you're through with it.
The same is true with the result from mysql_query() - close it as soon
as you're done.  It releases resources in the database as well as PHP.

Quoted text here. Click to load it

Or close the result set when you're through with it - like should be done.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline