Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Designing Interfaces in PHP Introducing Interfaces Implement MySQL RepositoryInterface

There does not seem to be enough information to complete this task - or am I missing something?

We have to implement the all() method of an interface which gets all the data from an SQL table passed to the function but it doesn't say whether this is just a string with the table name or something else. I tried running fetchAll(PDO::FETCH_OBJ) as suggested in the question but it want me to run a query on the database. To do this I believe I need a connection to the database but it doesn't tell me what the connection is or the information I need to establish my own connection.

sqlRepository.php
<?php
class sqlRepository extends PDO implements RepositoryInterface
{
  function all($table)
  {
    $db = 
    $results = $db->query("SELECT * FROM $table");
    return $results->fetchAll(PDO::FETCH_OBJ);
  }
}

1 Answer

I finally worked out that I could call the PDO functions statically, so this worked:

    $stmt = PDO::query("SELECT * FROM $table");