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 trialJamie Touton
7,051 PointsChallenge 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 = ?" );
<?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;
}
Jason Anello
Courses Plus Student 94,610 PointsHi 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
20,016 Pointsyou 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"
Jamie Touton
7,051 PointsHey 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
12,925 PointsIt's seems a strange practice to concatenate in this situation. Let it be this time...!
Jamie Touton
7,051 PointsThanks Peter! I'm pretty new, I wouldn't have thought of concatenating. :)
Peter Hatzer
20,837 PointsPeter Hatzer
20,837 PointsHi Jamie,
You almost have it, the variable in the function should be outside the string and concatenated on the end of it like so: