I’m wondering why my PDO statement returns 1 row but the data is empty when i assign it to a variable.
I have this:
$stmt = $pdo->prepare("SELECT count(uid) AS total,uid FROM accounts WHERE username = ? AND pass = ?");
try {
$stmt->execute(array($temp[0],$temp[1]));
} catch (PDOException $e) {
echo $e -> getMessage(); exit;
}
$row = $stmt -> fetch();
if(!$row['total']){
echo json_encode(0);
} else {
$_SESSION['userData'] = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($_SESSION['userData']);
}
When i echo the session it returns []
Is it because i do a fetch before the fetchAll ? If so how can i change it prevent that problem?
you can modify your code like this
currently what you are doing is first you check no. of rows by PDO::Fetch() and later you fetch that row with
PDOStatement::fetchAllyou can apply below approach which is more clear
First count the row using
PDOStatement::rowCount()and then use
PDOStatement::fetchto fetch the row.