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

PHP Interfaces, really bad challenge, does not provide enough info. How to prepare statement?

I have used PDO a number of time during development but this challenge makes absolutely no sense to me. I first got stuck on part 2 of this challenge because I tried to prepare a statement then execute, but that failed, so went with the 'query' method.

Now part 3 asks me to prepare a statement but I get a false return. I've no idea where to go from here as in my mind, I feel like i need to know the connection details in order to make a PDO object or am I missing something here?

sqlRepository.php
<?php
class sqlRepository extends PDO implements RepositoryInterface
{
    public function all($t)
    {     
      $s = $this->query("SELECT * FROM '$t'");
      $r = $s->fetchAll(PDO::FETCH_OBJ);
      return $r;
    }
  public function find($t, $v, $f = 'id')
  {
      $s = $this->prepare('SELECT * FROM :t WHERE :f = :v');
      $s->execute(array(':t' => $t, ':f' => $f, ':v' => $v));
      $r = $s->fetchAll(PDO::FETCH_OBJ);
      return $r;
  }
}
Patrik Horváth
Patrik Horváth
11,110 Points

if you use :name instant of "?" you have to add "array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)" into preper or not ?

$s = $this->prepare('SELECT * FROM :t WHERE :f = :v', array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$s->execute(array(':t' => $t, ':f' => $f, ':v' => $v));

5 Answers

Thanks for the reponse, appreciate you taking the time.

Never had to do that before, but tried it your way and still no joy. The prepare statement still returns a 'false' bool so the code doesn't get passed this point.

Patrik Horváth
Patrik Horváth
11,110 Points

you just Extended PDO class but you never make PDO object and you never connected to DB so maybe this is it ?

Without knowing the connections details, I'm not sure how to create a new PDO object which is why i just used the '$this'. Any idea's?

This is all the details the challenge provides:

Define the "find" method that accepts the required parameters. Write a prepared statement that selects only the items that match the table, value and field passed to the method. Make sure you bind the parameters to the statement. Return an array of query results.

Ok so I passed the challenge, but I'm pretty sure I cheated, wasn't on purpose just wanted to see what errors would be returned.

I basically did this:

$s = $this->prepare('SELECT * FROM :t WHERE :f = :v');

Changed to

$s = $this->prepare("SELECT * FROM $t WHERE $f = $v"); Then ran the execute. The code passed, but i'm pretty sure it shouldn't have as this in my mind is not the correct way of using PDO prepare and binding values or parameters

Simon Coates
Simon Coates
28,694 Points

I posted some code that seemed to work at https://teamtreehouse.com/community/im-stumped-on-this-one . Please feel free to take a look.