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 trialJohn Shockey
45,061 PointsStuck on Challenge Task 3; Uncaught Error: Call to a member function bindParam() on boolean in sqlRepository.php:14
- 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, $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
Simon Coates
28,694 PointsI was debugging someone else's code and had the same issue. There's some code at https://teamtreehouse.com/community/im-stumped-on-this-one . Or take a look at https://stackoverflow.com/questions/182287/can-php-pdo-statements-accept-the-table-or-column-name-as-parameter .