Hello,
I am using this very slightly modified function found here:
http://us2.php.net/mysql_fetch_object
to make mySQL rows into objects of type $classname:
// This takes db result rows and makes real objects of $classname type
function &buildObj($result, $classname) {
while($row = mysql_fetch_assoc($result)) {
if ($row === null) return null;
/* Create the object */
$obj =& new $classname;
/* Explode the array and set the objects's instance data */
foreach($row as $key => $value)
{
$obj->{$key} = $value;
}
$objs[] = $obj;
}
return $objs;
}
This is called by a retrieve: function:
function retrieve($where) {
echo $query = "SELECT * FROM $this->table WHERE " . join(' AND ',
$where);
$result = mysql_query($query);
$rows =& buildObj($result, get_class($this));
mysql_free_result($result);
return $rows;
}
The problem I am encountering is that if I do this in my script:
$listing = new Listing;
$listings = $listing->retrieve(array('row > 1') );
foreach ($listing as $aListing) {
$aListing->address='xxxxxxxxxxxxx';
}
foreach ($listing as $aListing) {
print_r($aListing);
}
The value in $aListing->address is unchanged.
However if I do this:
$listing = new Listing;
$listings = $listing->retrieve(array('row > 1') );
foreach ($listing as $aListing) {
$aListing->address='xxxxxxxxxxxxx';
$newArray[] = $aListing;
}
foreach ($newArray as $aListing) {
print_r($aListing);
}
I see the update values in $aListing->address .
What gives? Can anyone help me out here?
BTW I also tried removing the reference operator (&) from the whole thing
but it didn't help, I don't know why the author of the sub had used it.
TIA,
jg