Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Designing Interfaces in PHP Introducing Interfaces Implement MySQL RepositoryInterface

Stuck on Challenge Task 3; Uncaught Error: Call to a member function bindParam() on boolean in sqlRepository.php:14

  1. Define the "find" method that accepts the required parameters.
  2. 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.
  3. Return an array of query results.
sqlRepository.php
<?php

class sqlRepository extends PDO implements RepositoryInterface {
  public function all($entity){
    $db = new PDO("sqlite:".__DIR__."/database.db");
    $query = $db->query("SELECT * FROM ".$entity);
    $data = $query->fetchAll(PDO::FETCH_OBJ);
    return $data;
  }

  public function find($entity, $value, $field = 'id'){
      $db = new PDO("sqlite:".__DIR__."/database.db");
      $stmt = $db->prepare("SELECT * FROM :entity WHERE value = :value AND field = :field");
      $stmt->bindParam(':entity', $entity, PDO::PARAM_STR);
      $stmt->bindParam(':value', $value, PDO::PARAM_INT);
      $stmt->bindParam(':field', $field, PDO::PARAM_INT);
      $stmt->execute();
      $results = $stmt->fetch(PDO::FETCH_ASSOC);
      return $results;
  }
}

1 Answer