php - fetching data as new instance of existing class -


i have been trying learn code more oo , have been using pdo's. however, struggling find examples in documentation several pdo functions.

i using pdo's fetch_class function, , following code echoes each team's info per display() function. question fourfold;

1)is new instance of teaminfo class being created each time using pdo fetch_class?

2)if - each 1 named? or created teaminfo?

3)is there way reference particular instance of class in future code? using index value of array?

4)is there way declare name each new instance of class data fetched database - maybe using value of particular row each time (team_name example.

the code...

class teaminfo { public $team_name; public $aka; public $website; public $main_contact; public $phone; public $email; public $other;  public function display() {   $output='';   $output.=$this->team_name;   $output.='<br>' . $this->aka;   $output.='<br>' . $this->website;   $output.='<br>' . $this->main_contact;   $output.='<br>' . $this->phone;   $output.='<br>' . $this->email;   $output.='<br>' . $this->other;    return $output;  } }  require_once "old_pdo_enl_connect.php";  $database=dbnb_connect();  $query=$database->query("select team_name, aka, website, main_contact, phone, email,     other team_directory");   foreach ($query->fetchall (pdo::fetch_class, 'teaminfo') $team) {  echo $team->display() . "<hr>"; } 

1) yes.

2) objects assigned $team (which overwritten next object on each iteration) variable , class teaminfo

3) can save array of objects:

$rows = $query->fetchall(pdo::fetch_class, 'teaminfo'); 

and access every row:

$rows[0]->team_name;  

4) can create associated array team_name key:

$rows = array() foreach($query->fetchall(pdo::fetch_class, 'teaminfo') $row) {     $rows[$row->team_name] = $row; }  echo $rows['exampleteam']->website; 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -