This is my code. I am checking if a user exists or not in a login/registration system:
public function userExist($email){
$stmt = $this->conn->prepare("select email from users where email= ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows>0){
}
else{
}
}
Can I use get_result instead of store_result() ?
It depends on how you plan to read the result set. But in the actual example you have given, you are not interested in reading any returned data. The only thing that interests you is whether there is a record or not.
In that case your code is fine, but it would work equally well with get_result.
The difference becomes more apparent, when you want to get for example the userid of the user with the given email:
If you plan to read out that id with
$stmt->fetch
, then you would stick tostore_result
, and would usebind_result
to define in which variable you want to get this id, like this:If you prefer to get a result object on which you can call
fetch_assoc()
or any of the fetch_* variant methods, then you need to useget_result
, like this:Note that you get a result object from
get_result
, which is not the case withstore_result
. You should getnum_rows
from that result object now.Both ways work, and it is really a matter of personal preference.