I have this query:
public function checkUser($phone)
{
$sql = "SELECT name FROM users WHERE phone=:phone";
$stmt = db::getInstance()->prepare($sql);
$stmt->bindParam(':phone', $phone);
$stmt->execute();
$user = $stmt->fetchAll(PDO::FETCH_OBJ);
return $user;
}
And I call it like this:
$insertUser = $data->checkUser("1234567890");
print_r($insertUser);
What I need to know is if the query returns a user or not. When I call fetchAll() I get array(), but I need to be certain that a result is returned.
I tried to use $user = $stmt->rowCount(); but I get an error that says rowCount is an unknown function.
Any ideas on how to grab the number of rows and test whether there are rows returned or not?
Thanks
The easiest way given your current code would be:
Since Fetchall in your query returns ALL matching records in an array of one row per record or null if 0 records found, if the count is 0, easy–no user. If the count is greater than 0, at least one user is found.