You can lay out the PHP and HTML anyway you want. Ultimately its the end result that matters as well as consideration of when headers are pushed.
Output is the last thing I do. Takes more memory, but its easier to modify than simply outputting.
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php