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

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;
}

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