0

calling a variable outside of my while loop is getting an error what is the right way for calling the variable?

$gettingusername = $_GET['username'];
global $connectionDB;
$sql = "SELECT name, lastname FROM admin WHERE username=:username";
$stmt = $connectionDB->prepare($sql);
$stmt->bindValue(':username', $gettingusername);
$stmt->execute();
$result = $stmt->rowCount();
if($result==1){
    while ($fetch = $stmt->fetch()){
        $existingname = $fetch['name'];
        $existinglastname = $fetch['lastname'];
    }
}

<html>
<?php echo $existingname;?> //OUTPUT UNDEFINED VARIABLE HOW TO FIX IT?
<?php echo $existinglastname;?> //OUTPUT UNDEFINED VARIABLE HOW TO FIX IT?
</html>
vinz
  • 1
  • 1

1 Answers1

0

create variable before while and if

$gettingusername = $_GET['username'];
global $connectionDB;
$sql = "SELECT name, lastname FROM admin WHERE username=:username";
$stmt = $connectionDB->prepare($sql);
$stmt->bindValue(':username', $gettingusername);
$stmt->execute();
$result = $stmt->rowCount();
$existingname = '';
$existinglastname = '';
if($result==1){
  while ($fetch = $stmt->fetch()){
    $existingname = $fetch['name'];
    $existinglastname = $fetch['lastname'];
  }
}

<html>
<?php echo $existingname;?> //OUTPUT UNDEFINED VARIABLE HOW TO FIX IT?
<?php echo $existingname;?> //OUTPUT UNDEFINED VARIABLE HOW TO FIX IT?
</html>
Danz
  • 252
  • 1
  • 8