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
mysql_fetch_object & classes
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: mysql_fetch_object & classes
You can do what you want, just not with the constructor (which would run before mysql_fetch_object() assigned values). Probably something like this:
Better would be to do "SELECT id+100 AS myid FROM foo"
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;
}(#10850)
Re: mysql_fetch_object & classes
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?
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?