On Jul 31, 2007, at 2:00 AM, Richard Lynch wrote:
On Sun, July 29, 2007 5:49 pm, Ken Tozier wrote:
I'm trying to assign two fields of an object returned from a MySQL
query and have stumbled upon the most bizarre PHP bug where I can't
create two arrays in succession.
Here's the MySQL query with two dummy fields to be filled in later
select *, 0 as dummy_1, 0 as dummy_2 from table
Here's how I'm grabbing the query results
$result = array();
while ($row = mysql_fetch_object($query_result))
{
$result[] = $row;
}
Once that's done, I try to set two of the rows to arrays like this
$result-> dummy_1 = array(1, 2, 3, 4);
$result-> dummy_2 = array('a', 'b', 'c', 'd');
$result is an array and you are treating it like an object...
While this is supposed to work, I think, it's pretty confusing to this
naive reader...
$result['dummy_1'] = array(1, 2, 3, 4);
would make more sense...
That doesn't work. (I tried already) You have to use the "->" syntax
for objects returned by an MySQL query.
And you do realize that your actual objects are ELEMENTS of the array
$result, not $result itself...
So maybe you want something more like:
$row1 = $result[0];
$row1->dummy_1 = array(1, 2, 3, 4);
Yeah. My original post was incorrect. I'm actually doing something
more like your above suggestion
$result[0]->dummy_1 = array(1, 2, 3, 4);
I posted the complete function at 1:04 a.m. July 30 if you want to
take a look
That said, you're altering an Object that MySQL returned, and I've got
NO IDEA what kind of an object that is, or what you're allowed to cram
into its variables...
The weird thing is, I've been doing this type of assignment for a
couple of years, haven't upgraded my copy PHP for a year, but all of
a sudden it breaks. Very puzzling...
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php