One way to improve the organization of a procedural project is to separate php from html and break the code (macaroni) into functions. Also remember to use modern api’s for database connection avoiding mysql functions_*
Example:
macarrao.php
<?php
    $qr = "SELECT historico.*, funcionarios.nome FROM historico 
           INNER JOIN funcionarios
           ON (historico.funcionario_id = funcionarios.funcionario_id)";
    $resultado = mysql_query($qr);
?>
<table>
    <tr>
        <td>Nome</td>
        <td>Matricula</td>
        <td>Data entrada</td>
        <td>Data saida</td>
    </tr>
    <?php
    while($row = mysql_fetch_assoc($resultado)){
    echo
    '<tr>
        <td>'. $row['nome']  .'</td>
        <td>'. $row['matricula'] .'</td>
        <td>'. $row['entrada'] .'</td>
        <td>'. $row['saida'] .'</td>
    </tr>';
    ?>
</table>
1 - Remove the start code and the while from the.php folder and create a new file that can be sql/funcios.php, will stay that way:
include 'conexao.php';
function getHistorio($conexao){
    $sql = 'SELECT historico.*, funcionarios.nome FROM historico
            INNER JOIN funcionarios
            ON (historico.funcionario_id = funcionarios.funcionario_id)';
    $query = mysql_query($sql, $conexao) or die(mysql_error());
   $historicos = array();
   while($row = mysql_fetch_assoc($query)){
       $historicos[] = $row;
   }
    return $historicos;
}
//outras funções....
2 - Copy the html content of macaroni.php to a new, view/lista_historico.php file that will only have a foreach to list the histories
<?php
    include 'sql/funcionario.php';
    $historico = getHistorio($conexao);
?>
<table>
    <tr>
        <td>Nome</td>
        <td>Matricula</td>
        <td>Data entrada</td>
        <td>Data saida</td>
    </tr>
    <?php foreah($historico as $item){ ?>
    <tr>
        <td><?php echo $item['nome']; ?></td>
        <td><?php echo $item['matricula']; ?></td>
        <td><?php echo $item['entrada']; ?></td>
        <td><?php echo $item['saida']; ?></td>
    </tr>
  <?php } ?>
Recommended reading:
Flat PHP vs Symfony
Why should we not use mysql type functions_*?
Mysqli vs PDO - which is the most recommended to use?
							
							
						 
If they’re followed you can use the method multi-query. By the way I advise to use mysqli since mysql will be discontinued.
– Jorge B.