0

I cant get my function to work, I have next code

<?php
function alreadyExists($name) {
    require_once "connection.php";
    $funcsql = "SELECT * FROM `users` WHERE `name` = ?";
    $funcstmt = mysqli_stmt_init($conn);
    mysqli_stmt_prepare($funcstmt, $funcsql);
    mysqli_stmt_bind_param($funcstmt, "s", $name);
    mysqli_stmt_execute($funcstmt);
    $rows = mysqli_stmt_num_rows($funcstmt);
    if ($rows > 0 ) {
        $error1 = "Already exists";
        return $error1;
    }
}
?>

It is in the functions, and this in the my registration

$error1 = alreadyExists($login);
    if (isset($error1)) {
        $errors = "User already exists";
    }

But it dont work it will register you anyway, where is my error?

Vadim
  • 170
  • 11
  • @rohitt it is prepared statement – Vadim Feb 07 '21 at 17:21
  • `mysqli_stmt_num_rows` will always give you 0. You should perform `COUNT(*)` in SQL and get the result – Dharman Feb 07 '21 at 17:30
  • 1
    See my answer in the linked post. Forget about `mysqli_stmt_num_rows` completely. You do not need it. – Dharman Feb 07 '21 at 17:31
  • One obvious problem here is with your `isset`, which will evaluate as `true` since you're assigning it a value other than `null` to `$error1` in cases where user exists. `isset('Already exists') === true`. However, when a user *does not exist*, your function returns nothing, which when assigned to a variable, is `null`. In other words, the logic here is backwards. You should, rather than a string, return a boolean value, with `true` for *exists*, and `false` for *does not exist*. – Markus AO Feb 07 '21 at 18:15

0 Answers0