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

Stu Parker
Stu Parker
4,814 Points

PHP/SQL prepared statements

HI, cant figure where am going wrong.. have I done too many step in one... Appreciate the help

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

    try {
      $results = $db-> prepare
        query( "SELECT ?, email, fullname, level
          FROM members"
      );

      $results->bindParam(1,$member_id, PDO::PARAM_INT);
      $results->execute();
    } catch (Exception $e) {
      echo "bad query";
    }

    $members = $results->fetchAll();
    return $members;
}
Martin Balon
Martin Balon
43,651 Points

After keyword prepare delete curly brackets and query. $results = $db->prepare("SELECT ?, email, fullname, level FROM members")

Check the variable name you are binding to query "$memeber_id" - might be typo.

Also I'm not sure if you can put placeholder in query the way you did - check the documentation. http://php.net/manual/en/pdostatement.bindparam.php

2 Answers

Stu,

No worries I also had trouble briefly on this. When it refers to accepting 1 arguement for member_id it is asking for you to have the function accept that. The function it's referring to is the very top one:

function get_member()

So it should be function get_member($member_id)

Hope that helps!

Stu Parker
Stu Parker
4,814 Points

thanks bro, I came to that conclusion in.a.fit od inspiration yesterday

Sorry I couldn't have seen this sooner, just worked on this section today myself! Had help from a friend, was browsing for the next section an saw you question an figured I'd lend a hand!

Stu Parker
Stu Parker
4,814 Points

no probs..having problems on the next one as well..lol.. dont seem be adding anything to the array

Hey Stu,

Just checking to see if you were able to solve the next problem you had if not I'll do my best to lend a hand.

Stu Parker
Stu Parker
4,814 Points

still stuck lol

Stu Parker
Stu Parker
4,814 Points

while($row=$results->fetch(PDO::FETCH_ASSOC)) { $item["genres"][]=$row[genre_id["genre"]];

};

I think its close..

Stu Parker
Stu Parker
4,814 Points

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

try {
  $results = $db->prepare(
      "SELECT ?, email, fullname, level
      FROM members"
  );

$results-> bindParam(1,$member_id,PDO::PARAM_INT); $results->execute(); } catch (Exception $e) { echo "bad query"; }

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

} This spell checked code,lol, returns the error "your function should accept 1 argument for member_id" I was under the impression the second term in bindParam was this, with the first specifying which param to bind and the third the expected type..