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

I am not sure how can i bind the paramaters for a php call to the database.

How to bind params.


class sqlRepository extends PDO implements RepositoryInterface
    public function all($db)
        $sql = 'SELECT * from ' . $db;
        $stmt = $this->query($sql);
        return $stmt->fetchAll(PDO::FETCH_OBJ);

    public function find($db, $value, $field = 'id')
        $sql = $this->prepare('SELECT * from ' . $db . ' where ' . $field . '= ' . $value;

        $stmt = $this->query($sql);
        //$row = $stmt->fetch(PDO::FETCH_OBJ);
        //return $row; 
Simon Coates
Simon Coates
28,693 Points

I used string concatenation on the table and field and normal binding on the value. I think there are issues with binding on table names (see https://stackoverflow.com/questions/182287/can-php-pdo-statements-accept-the-table-or-column-name-as-parameter )

1 Answer

Henrik Christensen
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

Try have a quick look at the docs http://php.net/manual/en/pdostatement.bindparam.php :-)


/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
    FROM fruit
    WHERE calories < ? AND colour = ?');
$sth->bindParam(1, $calories, PDO::PARAM_INT);
$sth->bindParam(2, $colour, PDO::PARAM_STR, 12);