Forum Moderators: coopster

Message Too Old, No Replies

Why does this always validate to true?

         

realestatesteve

6:36 pm on Dec 23, 2008 (gmt 0)

10+ Year Member



So this code ALWAYS validates to true at the end, even if I load the page without any posts.

Why is that?


<?php
$connection = new mysqli('127.0.0.1', 'root', '', 'main');
$query = 'SELECT username, password FROM user WHERE username = ? AND password = ?';

$stmt = $connection->stmt_init();
if($stmt->prepare($query))
{
$stmt->bind_param('ss', $_POST['username'], $_POST['password']);
$stmt->execute();
}

if($stmt)
{
echo 'Your account was found!';
}
else
{
echo 'Your account was NOT found!...hacker';
}
?>

realestatesteve

6:46 pm on Dec 23, 2008 (gmt 0)

10+ Year Member



I think I got it; I see now that $stmt will always validate true since the query was executed. Rather, I needed to test to see how many results were returned from the query.

So I added:

$stmt->store_result();

And then changed the if statement to:

if($stmt->num_rows == 1)

And it works!

Is this the proper was to do this?

eelixduppy

6:57 pm on Dec 23, 2008 (gmt 0)



Should work. :)