It is about variable scope...
You variable is scoped or in the while or in the else part of your code and unknown for the HTML part...
Define them before the first if to make them scope for the entire code, including HTML...
Beside this... It seems to me that you have a serious logical glitch in your code...
See your code with my eyes...
if(whatever) {
...read data from database...
...loop over records from the database...
...push the current record into the variables...
}
else {
...set all variables to empty...
}
...display data...
With this code toy may display nothing or the last row from the database, which seems to me as the wrong option...