PHP Designing Interfaces in PHP Introducing Interfaces Implement MySQL RepositoryInterface

Amber Stevens
STAFF
Amber Stevens
Treehouse Staff

Stuck on this code challenge!!

1.Define the "all" method to return all items from the MySQL database table passed to the method. 2.Use "fetchAll(PDO::FETCH_OBJ)" to return the results as an array of objects.

At first I kept getting the error message: "Bummer: You should use PDO to execute a SQL query!"

So I made an SQL query and now it's telling me "Bummer: Uncaught Error: Call to a member function bindValue() on boolean in sqlRepository.php:8" I have been stuck trying to figure out this code challenge since yesterday. Any suggestions/help would be appreciated....

sqlRepository.php
<?php
class sqlRepository extends PDO implements RepositoryInterface
{
  public function all($table) 
  {
    $sql = "SELECT * FROM ?";
    $results = $this->prepare($sql);
    $results->bindValue(1, $table, PDO::PARAM_STR);
    $results->execute();
   return $results->fetchAll(PDO::FETCH_OBJ);

  }

}

1 Answer

Amber Stevens
STAFF
Amber Stevens
Treehouse Staff

Just wanted to update: It turns out all I needed to do to make this code work was to remove the bindValue and instead just add that value to my $sql string. Here's the code I finally got to work for me:

sqlRepository.php
<?php

class sqlRepository extends PDO implements RepositoryInterface
{
  public function all($table) 
  {
    $sql = "SELECT * FROM " . $table;
    $results = $this->prepare($sql);
    $results->execute();
   return $results->fetchAll(PDO::FETCH_OBJ);

  }

}