0

Am I doing something wrong with the code?

$stmt = $conn->prepare("SELECT nome FROM users WHERE nome = ?");
$stmt->bind_param('s', $_POST['name']);
$stmt->execute();
if ($stmt->affected_rows > 0) {
    if (verify_psw($psw_p, $nome) == true) {
        return true;
    }
} else {
    echo "<script>alert('nome errato');</script>";
}

here's the function that connect to the db:

function connect()
{
    $dbhost = "localhost";
    $dbuser = "site";
    $dbpass = "";
    $db = "my_site";
    $conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n". $conn -> error);
    return $conn;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
userone
  • 25
  • 6
  • Please share the error that you faced so that it's easier for the reader to get to the error straight and see what might be wrong. – Aashish gaba Jul 29 '20 at 10:57
  • the problem is that, there's no "error". but even when the post['name'] is equal to the name in the db the if return false – userone Jul 29 '20 at 11:00
  • in the frist if* – userone Jul 29 '20 at 11:08
  • Please show your mysql connection that is stored in $conn variable – Ariful Islam Jul 29 '20 at 11:12
  • From [mysqli_affected_rows](https://www.php.net/manual/en/mysqli.affected-rows.php) - *Returns the number of rows affected by the last INSERT, UPDATE, REPLACE or DELETE query.* – Nigel Ren Jul 29 '20 at 11:18
  • You have an error. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) – Dharman Jul 29 '20 at 11:40

1 Answers1

1

According to manual, mysqli_stmt::$affected_rows

Returns the number of rows affected by INSERT, UPDATE, or DELETE query.

Use mysqli_stmt::$num_rows to get the number of rows in return set.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Vesa Karjalainen
  • 1,087
  • 8
  • 15