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

Victoria Loisi
Victoria Loisi
7,070 Points

Adding a WHERE clause but it still results in error

The question states that I should "Add a WHERE clause to accept only the member that matches the passed member_id". And that's what I've done, but I still receive "Bummer! Use a WHERE statement to limit the results to a single member_id."

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

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

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

4 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,631 Points

Hey Victoria,

Your code is correct. The problem lies in that you modified some of the code that you shouldn't have between Task 1 and Task 2.

The line

$members = $results->fetch();

Originally was

$members = $results->fetchAll();

Change this line back to what it was and your code will pass.

Keep Coding! :) :dizzy:

Hazem mosaad
Hazem mosaad
15,384 Points

H Victoria the problem that you not pass the correct variable please see the comment blow

<?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 " // here the mistake ...
      );
    } catch (Exception $e) {
      echo "bad query";
    }

    $members = $results->fetchAll();
    return $members;
}
Jason Anders
Jason Anders
Treehouse Moderator 145,631 Points

Hey Hazem mosaad

Actually, the variable name doesn't matter for this challenge. The problem was that fetchAll() was changed to fetch(). Using $id will pass the challenge. :) :dizzy:

Victoria Loisi
Victoria Loisi
7,070 Points

By the way, here's the answer for the other challenge:

SELECT * FROM Media 
JOIN Media_Genres ON Media.media_id = Media_Genres.media_id
JOIN Genres ON Media_Genres.genre_id = Genres.genre_id
WHERE Media.media_id=3;

You need to Join media to Genres, but tehre's no direct connection between them, so you need to joing Media to Media_Genres first, and then, Media_Genres to Genres. Also, you need to use Media.media_id=3 instead of media_id=3 because SQL won't know which of the "media_id" fields you are referring to.