vendredi 31 juillet 2015

How to check if query returned true or false in replacement for mysql_error() in PDO

I am creating a web based application, and I am using PDO for my database. I have a query that selects everything from login table where username=something and password=something.

My code:

$query = $db->prepare("SELECT * FROM login WHERE username=:username AND password=:password");
$query->bindParam(':username',$username);
$query->bindParam(':password',$password); 
$query->execute();

However I want to check if the query returned true or false. For example in mysql we used to say:

$query = mysql_query("SELECT * FROM login WHERE username='$username' AND password='$password'  "); 
if($query == false){
    die(mysql_error()); 
}

My question is, how do I check if the query returned false or true using PDO and gives an error? This will help me get errors on my code during development.

What am i going to replace with mysql_error()?

Aucun commentaire:

Enregistrer un commentaire