You have to execute the store_result
and preferably if you’re going to use OOP then use everything like this, there’s no reason why in the mysqli API to mix procedural with OOP (I’m only talking about this API, the rest of PHP is a matter of taste and need, so you can "mix"):
if ($stmt = $mysqli->prepare("SELECT * FROM usuario WHERE login='$login' and senha='$senha';")) {
/* execute query */
$stmt->execute();
/* store result */
$stmt->store_result();
if ($stmt->num_rows < 1) {
echo "<script language='javascript' type='text/javascript'>alert('Login e/ou senha incorretos');window.location.href='Teste.php';</script>";
} else {
setcookie("login", $login);
header("Location:index.php");
}
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
Another very important thing, why use variables with prepare
directly in the string? (no need to answer)
If you want to prevent someone from passing a login or password value that causes syntax error or even a sql-injectcion use directly the bind_param
, because that’s the purpose of prepare
, thus:
if ($stmt = $mysqli->prepare("SELECT * FROM usuario WHERE login=? and senha=?")) {
/* passa os valores na ordem dos interrogações */
$stmt->bind_param('ss', $login, $senha);
/* execute query */
$stmt->execute();
/* store result */
$stmt->store_result();
if ($stmt->num_rows < 1) {
echo "<script language='javascript' type='text/javascript'>alert('Login e/ou senha incorretos');window.location.href='Teste.php';</script>";
} else {
setcookie("login", $login);
header("Location:index.php");
}
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
the user you are using exists in the comic? is sure the password is correct?
– Luís Almeida
Yes. the user is 'test' and the password too, I tried to enter directly with: SELECT * FROM user WHERE login='test' and password='test';. But still it returns 0.
– Kahzinhuh