PHP undefined variable error

Associate
Joined
11 Oct 2008
Posts
268
Hello everyone. Please could some shed some light onto why I am getting this undefined variable error. Thanks for any tips :)

I have cobbled together a login script using the new php hashing and verify functions.

I am getting this error:
PHP:
Undefined variable: db_password in C:\wamp\www\login\index.php on line 207

Line 207 is the following:
PHP:
if(password_verify($password, $db_password)) {

And here is the full code:

PHP:
    $username = '';
    if(isset($_POST['username']))
    $username = htmlentities($_POST['username']);

    $password = '';
    if(isset($_POST['password']))
    $password = $_POST['password'];    

	$sql = "SELECT * FROM login WHERE username=?"; 
	$get = $connect->prepare($sql);
	$get->execute(array($username)); 
	
	if($get->rowCount() === 1)
	{
		$row = $get->fetch(PDO::FETCH_ASSOC); // Fetch the result
		
		$db_username = $row['username'];
		$db_password = $row['password']; 

	}  

	if(password_verify($password, $db_password)) { 
 
`	$_SESSION['username'] = $db_username;
      
	$sql = "UPDATE login SET last_login=?, ip=? WHERE username=?";
	$statement = $connect->prepare($sql);
	$statement->execute(array($dt,$ip,$username)); 
	
	}
 
Associate
OP
Joined
11 Oct 2008
Posts
268
ah ok. thanks.

I have removed if($get->rowCount() === 1) and it seems to be working now.

is it okay that I have removed that. Was it doing anything important?

if i echo the row count its coming up 0 but there is a row in the table, and the login works. Just spits out the error
 
Last edited:
Associate
OP
Joined
11 Oct 2008
Posts
268
okay I put it back in.

But if the rowcount is 0 is it actually worth putting back in? is it doing anything?

From what I have read, it only returns a row number if I do something that effects the table, so only checking the posted username against the database one shouldnt return anything?
 
Back
Top Bottom