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

Matthew OToole
Matthew OToole
22,946 Points

Why would I be getting an array of nulls here?

I'm stuck on Q3 of the Designing Interfaces with PHP section.

Challenge Task 3 of 3

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

<?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, $id = 'id'){
      $db = new PDO("sqlite:".__DIR__."/database.db");
      $stmt = $db->prepare("SELECT * FROM $entity WHERE $value = ? AND $id = ?");
      $stmt->bindParam(':entity', $entity, PDO::PARAM_BOOL);
      $stmt->bindParam(':value', $value, PDO::PARAM_INT);
      $stmt->bindParam(':id', $id, PDO::PARAM_INT);
      $stmt->execute();
      $results = $stmt->fetchAll(PDO::FETCH_OBJ);
      return $results;
  }
}

Bummer! The results of the "find" method should return an array of objects. The current results are an array of NULLs

mark winters
mark winters
1,485 Points

You have $stmt = $db->prepare("SELECT * FROM $entity WHERE $value = ? AND $id = ?");

this should be $stmt = $db->prepare("SELECT * FROM $entity WHERE value = ? AND id = ?"); I think.

You have used the $value and $id as column names because there are being used in the " " quotes. The $entity looks good. It is the same as "SELECT * FROM ".$entity in you're all method.

1 Answer

Matthew OToole
Matthew OToole
22,946 Points

Thanks, but that didn't appear to work. Stuck on this one.