PDO Query issue

Hello,

i am banging head now for hours. coz the query not fetching the ‘status’ column data :frowning:

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

$row_count = $query->fetchColumn();
if ($row_count < 1){
   echo "Sorry. Username and Password does not match. Please try again." ;
   exit;
}

//check weather account is active//
$row = $query->fetch();
echo $row['status']; //here status returnes blank while it has the value 1.

the status is blank because you only have one result row which you process using fetchColumn(). applying a second fetch returns false.

Hi dormilich,

That mean can’t i fetch() a query more than one time?

  1. row_count = $query->fetchColumn();
  2. $row = $query->fetch();

Thanks Dormilich,
i just modified the code and it works now.

//check user name and password//
$query = $db->prepare("SELECT COUNT(username) 
                       FROM users
                       WHERE username=:username 
                       AND password=:password");
                       
$query->execute(array(':username' => $username,
                      ':password' => $password));

$row_count = $query->fetchColumn();
if ($row_count < 1){
   echo "Sorry. Username and Password does not match. Please try again." ;
   exit;
}


//Featch Data
$query = $db->prepare("SELECT * 
                       FROM users
                       WHERE username=:username 
                       AND password=:password");

$query->execute(array(':username' => $username,
                      ':password' => $password));

//check weather account is active//
$row = $query->fetch();
$status=$row['status'];

echo $status;

there’s no need for the first query at all. if you have no match, fetch() will return false.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.