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

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

How to bind params.

sqlRepository.php
<?php

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);
        var_dump($stmt);
        //$row = $stmt->fetch(PDO::FETCH_OBJ);
        //var_dump($row);
        //return $row; 
    }
}
Simon Coates
Simon Coates
28,694 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
seal-mask
.a{fill-rule:evenodd;}techdegree
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 :-)

example

<?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);
$sth->execute();
?>