Page 1 of 1

mysql_fetch_object & classes

Posted: Sat Nov 21, 2009 1:16 pm
by oleg
Hello

I'm trying to retrieve information from Mysql using mysql_fetch_object and store each row as an object.

I know that I can use something like this, but is not stored in my class
while ($row = mysql_fetch_object($result))
{
echo $row->id;
}

I need to store the fields in a class to make some changes in the values.

class className{
var $newId;
function className()
{
$this->newId = $id + 100;
}
}

while ($row = mysql_fetch_object($result, "className"))
{
echo $row->newId;
}

The thing is that the constructor of the class is not doing anything and I dont know how to store the $id that comes from mysql into my class.

Thanks in advance

Re: mysql_fetch_object & classes

Posted: Sat Nov 21, 2009 7:29 pm
by Christopher
You can do what you want, just not with the constructor (which would run before mysql_fetch_object() assigned values). Probably something like this:

Code: Select all

class className{
   var $newId;
    function changeSomeValues()
    {
         $this->newId = $id + 100;
    }
}
 
while ($row = mysql_fetch_object($result, "className"))
{
        $row->changeSomeValues();
    echo $row->newId;
}
Better would be to do "SELECT id+100 AS myid FROM foo"

Re: mysql_fetch_object & classes

Posted: Sun Nov 22, 2009 3:39 am
by oleg
Thanks for the quick response.

When I create an object with mysql_fetch_object the name variables must be the same (object / mysql query)?
In the example I must create a table with the element newId and in the className one variable called $newId?

Is it possible to run the function changeSomeValues() from the constructor?