Jump to content

Object oriented results?


GuitarGod

Recommended Posts

well in your first example

 

$row[field_name'];

 

$row is an array and field name is the key for which item out of the array you want.

 

your second example

 

$row->field_name

 

$row would usually be an object you have created of a class.

 

and the field_name would be a method available to that class.

 

So unless your field_name function retrieved an array of data from your DB i guess the answer is no

mysql_fetch_object is the way to go, but on a side note, PHP has a built in stdClass. so if you have an array want want to make an object out of it:

 

<?php
$array = array(
  'foo' => 'bar',
  'test' => 'value',
);

$obj = new stdClass();
foreach($array as $k=>$v){
  $obj->$k = $v;
}

echo $obj->foo;
?>

Another side note. mysql_fetch_object can actually return a pre-defined object type. eg;

 

<?php

  class user {
    
    public $uname;

    public function get_uc_uname() {
      return strtoupper($this->uname);
    }

  }

  $sql = "SELECT uname FROM users WHERE uid = 2";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $user = mysql_fetch_object($result,'user');
      echo $user->get_uc_uname();
    }
  }

?>

 

This can come in pretty handy.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.