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

Challenge Task 2 of 3: Add a WHERE clause to accept only the member that matches the passed member_id.

Can anyone help me with this? I'm not sure how to do this. Originally I thought I should write it like: "....WHERE members.members_id = ?" );

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

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

    $members = $results->fetchAll();
    return $members;
}
Peter Hatzer
Peter Hatzer
20,837 Points

Hi Jamie,

You almost have it, the variable in the function should be outside the string and concatenated on the end of it like so:

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

    try {
      $results = $db->query(
          "SELECT member_id, email, fullname, level
          FROM members
          WHERE member_id = " . $member_id
      );
    } catch (Exception $e) {
      echo "bad query";
    }

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

Hi Jamie,

There may have been a problem with the challenge when you tried this code. The code you have posted passes task 2.

You could use concatenation but in this case it's not necessary since the variable is inside double quotes and it will be interpreted.

3 Answers

Steve Berrill
Steve Berrill
19,994 Points

you dont need to concatenate, just add the variable in the query string

"SELECT member_id, email, fullname, level FROM members WHERE member_id = $member_id"

Hey Jason,

It definitely didn't pass with my code. But it's nice to know both of those ways work. Now the question is, when to concatenate and when not to concatenate?

Marius Unknown
Marius Unknown
12,925 Points

It's seems a strange practice to concatenate in this situation. Let it be this time...!

Thanks Peter! I'm pretty new, I wouldn't have thought of concatenating. :)