PHP Designing Interfaces in PHP Introducing Interfaces Implement MySQL RepositoryInterface

mcutz
mcutz
7,862 Points

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,070 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

mcutz
mcutz
7,862 Points

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,070 Points

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

mcutz
mcutz
7,862 Points

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?

mcutz
mcutz
7,862 Points

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.

mcutz
mcutz
7,862 Points

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,651 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.