fetching data as new instance of existing class

I have been trying to learn to code more OO and have been using PDO's. However, I am struggling to find examples in the documentation for several PDO functions.

I am using PDO's fetch_class function, and the following code successfully echoes each team's info as per the display() function. My question is fourfold;

1)Is a new instance of the TeamInfo class being created each time using PDO fetch_class?

2)If so - what is each one named? Or are they all created as just TeamInfo?

3)Is there a way to reference a particular instance of the class in future code? Like using the index value of an array?

4)Is there a way to declare a name for each new instance of the class as the data is fetched from the database - maybe using the value of a particular row each time (team_name for 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.='<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";


$query=$database->query("SELECT team_name, aka, website, main_contact, phone, email,     other from team_directory");

foreach ($query->fetchAll (PDO::FETCH_CLASS, 'TeamInfo') as $team) {

echo $team->display() . "<hr>";


1) Yes.

2) The objects are assigned to $team (which is overwritten by next object on each iteration) variable and the class is TeamInfo

3) You can save array of objects:

$rows = $query->fetchAll(PDO::FETCH_CLASS, 'TeamInfo');

And access to every row:


4) You can create associated array with team_name as key:

$rows = array()
foreach($query->fetchAll(PDO::FETCH_CLASS, 'TeamInfo') as $row) {
    $rows[$row->team_name] = $row;

echo $rows['ExampleTeam']->website;

Need Your Help

window.location.hash not working in Chrome/Safari

javascript jquery webkit

I have the following code on my website, which basically checks to see if there is a hash in the URL, and if there is then it triggers a click on a tab. This works fine apart from in Chrome/Safari ...

Create a percentage with three numbers

javascript jquery math

I'm trying to write a small script that will convert three numbers into three percentages. Jquery can set the width of three coloured divs to reflect the 'spread' of these numbers.