PDA

View Full Version : PDO select



ycpc55
08-01-2012, 03:54 PM
Hi,
i was wondering if this is the right way to select from the database using pdo. Also should this line be at the end or no $row = $stmt->fetch();. Thanks.


$query = "
SELECT
id,
username,
password,
salt
FROM users
WHERE
username = :username
";

$query_params = array(
':username' => $_POST['username']
);

try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{

die("Failed to run query: " . $ex->getMessage());
}

$row = $stmt->fetch();

Dormilich
08-02-2012, 07:49 AM
without one of the fetch methods you have no access to the data.

PS. the try...catch block only makes sense if you set PDO’s error handling to Exceptions