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

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
20,016 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. :)