简体   繁体   中英

PHP Prepared statement returning wrong value

I am trying to find out if a user exists in my database already, but no matter what I try, I always get the equivalent of a false.

$stmt = $connection->prepare("SELECT * FROM members WHERE member_username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();

if($stmt->num_rows > 0) {
    return 'user exists';
}

I know this has got to be something very simple... something I am clearly overlooking, but I always get that num_rows = 0, even though the user is in the database already.

Can someone please help?

Call get_result() or store_result() before call num_rows :

$stmt = $connection->prepare("SELECT * FROM members WHERE member_username = ?");    
$stmt->bind_param("s", $username);
$stmt->execute();
$res = $stmt->get_result(); //<--- HERE

if($res->num_rows > 0) { //<--- HERE
    return 'user exists';
}

OR

$stmt->execute();
$stmt->store_result(); //<--- HERE

if($stmt->num_rows > 0) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM