mysql_fetch_object & classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oleg
Forum Newbie
Posts: 6
Joined: Thu Aug 28, 2008 2:51 am

mysql_fetch_object & classes

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: mysql_fetch_object & classes

Post 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"
(#10850)
oleg
Forum Newbie
Posts: 6
Joined: Thu Aug 28, 2008 2:51 am

Re: mysql_fetch_object & classes

Post 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?
Post Reply