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

Robert Walker
Robert Walker
17,146 Points

PDO and select OOP

try{

$statement =  $db->prepare('SELECT * FROM users WHERE email= :email');
$statement->bindParam(":email", $email, PDO::PARAM_STR);
$statement->execute();
$row = $statement->fetch(PDO::FETCH_ASSOC);

} catch (PDOException $e) {

    // handle error message etc

}

Simple PDO select statement but im looking to build a more OOP version and ive seen people use the "?" and also loop through an array to bind param.

I know I could do something like table, fields and action to build the statement but im not sure how to handle the binding params.

$q = $dbc -> prepare("INSERT INTO accounts (username, email, password) VALUES (?, ?, ?)");
$q->execute(array($_POST['username'], $_POST['email'], $_POST['password']));

There is this version ive seen too but this doesn't bind the param as a string.

I'm just looking for advice and help on which is the correct way and what the pros and cons might be of doing your suggested version.

Any help would be amazing thank you!