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

Amber Stevens
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Amber Stevens
Treehouse Project Reviewer

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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Amber Stevens
Treehouse Project Reviewer

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);

  }

}