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