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

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