Jump to content

MySQLiWrapper query


blacklion

Recommended Posts

Hi All,

 

I am having some trouble understanding a bit of code which extends MySQLi.

I am trying to move into using MySQLi and at the same time into a more OO approach so forgive me if this is trivial!

 

The code is thus (courtesy of the php.net user contributed notes)

 

class Database_MySQLi extends MySQLi
{
    public function query($query)
    {
        $this->real_query($query);
        return new Database_MySQLi_Result($this);
    }
}

class Database_MySQLi_Result extends MySQLi_Result
{
    public function fetch()
    {
        return $this->fetch_assoc();
    }

    public function fetchAll()
    {
        $rows = array();
        while($row = $this->fetch())
        {
            $rows[] = $row;
        }
        return $rows;
    }
}

 

I assume this is used as follows (as in another very similar example I have);

 

$db = new Database_MySQLi(.....);

$users = $db->query("SELECT * FROM USERS");

foreach ($users as $user) {

    echo $user->firstname;

    echo $user->lastname;

}

 

I understand what is happening, what I don't understand is how does it know what function ( fetch() / fetch_all() / fetch_object() [as in example] ) to use from the Database_MySQLi_Result class?

Thanks for any tips!

 

BL

 

Link to comment
https://forums.phpfreaks.com/topic/190109-mysqliwrapper-query/
Share on other sites

It doesn't. Code should look more like this:

 

$db = new Database_MySQLi(.....);
$users = $db->query("SELECT * FROM USERS");
while($user = $users->fetch()) {
    echo $user->firstname;
    echo $user->lastname;
}

 

or

 

$db = new Database_MySQLi(.....);
$result= $db->query("SELECT * FROM USERS");
$users = $result->fetchAll();
foreach ($users as $user) {
    echo $user->firstname;
    echo $user->lastname;
}

Link to comment
https://forums.phpfreaks.com/topic/190109-mysqliwrapper-query/#findComment-1003068
Share on other sites

Thanks Mchl. Your explanation makes perfect sense.

 

However, (I should have explained to start with) my actual query is with a MySQLi_ResultWrapper class which uses the iterator and countable interfaces. It seems to work without being explicitly told to retrieve any one function or another - that I can see!

The class in question is written by Alejandro Gervasio over at DevShed :

http://www.devshed.com/c/a/PHP/Improving-MySQL-Connection-with-Static-Variables-in-PHP-5-Classes/1/

 

BL

Link to comment
https://forums.phpfreaks.com/topic/190109-mysqliwrapper-query/#findComment-1003075
Share on other sites

Thanks Mchl.

I haven't come across Iterators yet really but it makes sense now!

About your comment, it is a must to have a constructor in this class?

This class seems quite tidy and a good place to start playing with mysqli interface but maybe not...?

 

BL

 

Link to comment
https://forums.phpfreaks.com/topic/190109-mysqliwrapper-query/#findComment-1003140
Share on other sites

Each class have a default constructor that does nothing. Well... not really... It calls constructor from parent class if it's present. I forgot about it, and hence my confusion. (Because 'user-defined' constructors do NOT call parent constructors).

 

http://www.php.net/manual/en/language.oop5.decon.php

Link to comment
https://forums.phpfreaks.com/topic/190109-mysqliwrapper-query/#findComment-1003161
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.