PHP PDO num_rows issue -
when i'm trying log in using existing account code runs ok when attempt log in non-existent account should else
part , echo
message still part defined in if
block. why?
any other tips welcome
$sql = "select email,password,id user email=? , password=?"; $stm = $db->prepare($sql); $stm->execute(array($email,$password)); $row = $stm->fetch(pdo::fetch_assoc); $number_of_rows = $stm->fetchcolumn(); if(count($number_of_rows)==1){ $_session['id']=$row['id']; header('location: cpanel/#welcome'); }else{ echo "<font style='background-color: #f9c5ca'>error: email and/or password incorrect. please try again.</font>"; }
fetchcolumn()
not count number of rows, returns specific column value next record in result set (or false
if @ end of result set). if using mysql can use rowcount()
count rows in result set (this method not supported select
statements across database types however).
alternately, since seems care 1 row being returned, can evaluate value of $row
. if false
didn't record returned. might suggest adding explicit limit 1
query clear reader of code 0 or 1 records ever returned query.
Comments
Post a Comment