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 Integrating PHP with Databases Using Relational Tables Using a Prepared Statement

Problem with prepared statement task

In challenge "Change the query to a prepared statement that binds the passed argument before executing the query."

I wrote this code:

<?php function get_member($member_id) { include("connection.php");

try {
  $results = $db->prepare(
      "SELECT member_id, email, fullname, level
      FROM members
      where member_id = ?"
  );
  $results = bindParam(1,$member_id);
} catch (Exception $e) {
  echo "bad query";
}

$members = $results->fetchAll();
return $members;

}

And I get this answer:

"Bummer! Uncaught Error: Call to undefined function bindParam() in index.php:11"

But this method (miscalled in error as undefined function) is part of PDO class and instanced by $results

index.php
<?php
function get_member($member_id) {
    include("connection.php");

    try {
      $results = $db->prepare(
          "SELECT member_id, email, fullname, level
          FROM members
          where member_id = ?"
      );
      $results = bindParam(1,$member_id);
    } catch (Exception $e) {
      echo "bad query";
    }

    $members = $results->fetchAll();
    return $members;
}

2 Answers

Daniel Pérez
Daniel Pérez
13,402 Points

This line is the problem: $results = bindParam(1,$member_id);

"bindParam" is a method of the pdo object, so write something like this:

$results->bindParam(1, $member_id, PDO::PARAM_INT);

Forget it!!! I saw my mistake: $results = bindParam(1,$member_id);

instead of: $results->bindParam(1,$member_id);