By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,441 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,441 IT Pros & Developers. It's quick & easy.

accessing an stdClass Object

P: n/a
red
If I use PEAR to produce an accociative array from a mysql table, and
display the array with print_r, it says it is an array and it displays
values like a regular array:

Array
(
[id] => 1
[datatype] => image/gif
[name] => buffaloa.gif
[size] => 153473
[filedate] => 2004-10-02 07:03:43
)
But if I try to access the members of the array, they act like
stdClass objects. print_r produces something like this:
stdClass Object
(
[:private] => buffaloa.gif
)

echo simply says it is "Object id #1"

I would like to access the members of the array but I don't see how.

below is the code I am using and the output
db() is just my debugging function which uses print_r for output

code:
$mode=DB_FETCHMODE_ASSOC;
$sql="select * from $table";
echo t(1)."Table:$table$n";
$res = $db->query($sql);
while($trows = $res->fetchRow($mode)){
db($trows,'trows'); //output looks like an array
foreach($trows as $trow->$data){
db( $trow,'trow'); // output looks like an stdClass Object
db($data,'data'); // output is empty
}
}

output:
trows Array
(
[id] => 1
[datatype] => image/gif
[name] => buffaloa.gif
[size] => 153473
[filedate] => 2004-10-02 07:03:43
)
trows

trow stdClass Object
(
[:private] => 1
)
trow

data data

trow stdClass Object
(
[:private] => image/gif
)
trow

data data

trow stdClass Object
(
[:private] => buffaloa.gif
)
trow

data data

trow stdClass Object
(
[:private] => 153473
)
trow

data data

trow stdClass Object
(
[:private] => 2004-10-02 07:03:43
)
trow

data data
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
red wrote:
code:
$mode=DB_FETCHMODE_ASSOC;
$sql="select * from $table";
echo t(1)."Table:$table$n";
$res = $db->query($sql);
while($trows = $res->fetchRow($mode)){
db($trows,'trows'); //output looks like an array
foreach($trows as $trow->$data){
db( $trow,'trow'); // output looks like an stdClass Object
db($data,'data'); // output is empty
}
}


You are using the wrong operator in foreach, "->" should be "=>":

foreach($trows as $trow => $data){
...
}
JW

Jul 17 '05 #2

P: n/a
red
Janwillem Borleffs wrote:
red wrote:
code:
$mode=DB_FETCHMODE_ASSOC;
$sql="select * from $table";
echo t(1)."Table:$table$n";
$res = $db->query($sql);
while($trows = $res->fetchRow($mode)){
db($trows,'trows'); //output looks like an array
foreach($trows as $trow->$data){
db( $trow,'trow'); // output looks like an stdClass Object
db($data,'data'); // output is empty
}
}

You are using the wrong operator in foreach, "->" should be "=>":

foreach($trows as $trow => $data){
...
}
JW

OK... It all makes sense now. Its just a stupid mistake. Whew. I
couldn't understand why a key-value of an array is suddenly an object. I
thought it was some kind of PHP or PEAR consrtuct I never heard of. I
guess key-value pairs are always objects. Thanks
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.