0

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 i.e:

- 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;
}
3
  • Check for mysqli_num_rows: 0 if not exists, 1 if exists. Commented Mar 31, 2013 at 13:13
  • @enapupe: in checkUserFound function? or in if($this->checkUserFound($email) != true ){die();} ?! Commented Mar 31, 2013 at 13:21
  • Inside checkUserFound, it should return true (or user ID) only when the SQL has one result row.. I don't know the class you are using, but you must have some funcion like $stmt->num_rows().. Anyway, why are you always return false on your function?? Commented Mar 31, 2013 at 13:25

2 Answers 2

1

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)./

Sign up to request clarification or add additional context in comments.

Comments

0
if ($stmt->execute()) {

        $stmt->bind_result($email);

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

    return false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.