简体   繁体   中英

PHP: check row exist using mysqli query

i have this two function for create user.first function for insert/create user and other function for check if user was/exist in MySql database. but when i send data i see blank page for die(); (if user exsit or user not exist) how to fix this error? and how to print error list in red box ie:

- this user not activate 

- email is empty 

- ....

my class:

public function createUser($email, $password, $salt, $verification) {

if($this->checkUserFound($email) != true ){die();}

    $query = "INSERT INTO tbUsers (email, password, user_salt, is_verified, is_active, is_admin, verification_code) "
    . "VALUES (?, ?, ?, ?, ?, ?, ?)";

    $stmt = $this->_db->prepare($query);

    $ver = 0;
    $act = 1;
    $adm = 0;

    $stmt->bind_param("sssiiis", $email, $password, $salt, $ver, $act, $adm, $verification);

    if ($stmt->execute()) {
        return true;
    }

    return false;
}

public function checkUserFound($email) {
    $query = "SELECT email FROM tbUsers where email = ?";

    $stmt = $this->_db->prepare($query);

    $stmt->bind_param("s", $email);

    if ($stmt->execute()) {

        $stmt->bind_result($email);

        //fetch first row of results
        $stmt->fetch();
        return false;
    }

    return true;
}

This is always going to return FALSE (assuming the query successfully executes):

if ($stmt->execute()) {

    $stmt->bind_result($email);

    //fetch first row of results
    $stmt->fetch();
    return false;
}

You want to change it to:

if ($stmt->execute()) {

    $stmt->store_result();


    return 0 == $stmt->num_rows ? FALSE : TRUE;

}

Then, if the user is found, it will return TRUE ; if not if will return FALSE .

Also, I think it make more sense (semantically) to have this:

if($this->checkUserFound($email)){die();}

... because you want it to return TRUE if the user is found (and end the script because you don't want to insert a duplicate)./

if ($stmt->execute()) {

        $stmt->bind_result($email);

        //fetch first row of results
        $stmt->fetch();
        return true;
    }

    return false;

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