473,396 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Changing data in Objectified mysql records

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


Jul 17 '05 #1
1 1604
My error was trying to act on $this when I had passed $obj.

jg
"jerrygarciuh" <de*****@no.spam.nolaflash.com> wrote in message
news:p4yKc.2819$fB4.622@lakeread01...
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

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Jerry Weinstein | last post by:
Hi, I know about stored procedures and that they can speed up data entry via the SQL 'insert ' statement. However, one drawback to this method is that using the stored procedure still requires...
2
by: Alex Hunsley | last post by:
I'm using a mysql monitor under cygwin (on win xp) to do a 'load data infile' to put some data into a mysql database (I'm using the xampp bundle).. My problem is that I have a four line CSV file...
1
by: Tony W. | last post by:
I have a MySQL question about changing the info in a table: I am using MySQL Server version: 3.23.58 I have a table called sign_region2 and it contains thousands of references to signs and the...
1
by: trt08010 | last post by:
I'm new to MySQL so hopefully this all makes sense. I'm running mysql-3.23.58-14 on Fedora Core 3 and everything installs and comes up correctly using default locations for everything. However,...
3
by: hamvil79 | last post by:
I'm implementig a java web application using MySQL as database. The main function of the application is basically to redistribuite documents. Those documents (PDF, DOC with an average size around...
2
by: S P Arif Sahari Wibowo | last post by:
Hi! Do you know how to put a form's Access-Visual-Basic-code that will force the form to be inserted, while the user has not type anything in the form, without changing focus, selection, etc.? ...
12
by: VMI | last post by:
For some reason, the process of retrieving data (about 20 records) from an Access table that has 400K records to a dataTable is taking over 3 mins. to complete. Below is my code to connect to the...
4
by: Good Man | last post by:
Hi there I have a database with about 20 or so tables, maybe a few thousand rows in each. I am starting to do more complex things with my insertions etc, and I want to start to use...
7
by: Ming | last post by:
For example, 100,000 records, each record has 10 fields and may belong to one or more categories. How shall I save those records in DB? I think it is a very typical concern for most online shopping...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.