First of all, you are having an issue Id refer to as 'variable scope'. You are defining a function gettab() without passing any arguments, while the variable $username is undefined as a local variable inside your function. You either have to declare $username as global variable, or pass it as an argument to your function. Read this manual from PHP for reference:
http://php.net/manual/en/language.variables.scope.php
On the other hand, you still use mysql_result() which no longer works with this new script as we are incorporating PDO. I thought Fadillzzz already explained clearly to you not to use mysql functions anymore... Now change this line:
PHP Code:
$tabname=@mysql_result($data, $i,"name");
to
PHP Code:
$tabname=$row->name;
And now the $tabname will not be malfunctioning. However I strongly recommend you to remove all lines associated with $i since its totally unnecessary.