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

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,860 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,860 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.