How to deal with multiple queries?

5

I have a question regarding good programming practice. How to handle multiple queries on a single page in PHP. For example, the code:

    $qr = "SELECT historico.*, funcionarios.nome FROM historico INNER JOIN funcionarios ON (historico.funcionario_id = funcionarios.funcionario_id)";
    $resultado = mysql_query($qr);

Soon below on my page, I have to do another query:

    $qr2 = "SELECT diasplantao.*, funcionarios.nome FROM diasplantao INNER JOIN funcionarios ON (diasplantao.funcionario_id = funcionarios.funcionario_id)";      
    $resultado2 = mysql_query($qr2);

And so on, and there may be others. My question is: What is the best way to manage this? Is there any way to make the code not get so messy with so many variables.

    
asked by anonymous 20.05.2014 / 04:59

3 answers

5

One way to improve the organization of a procedural project is to separate the php from the html and break the code (noodles) into functions. Also remember to use modern api's for database connection by avoiding the 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 code of the start and while of macarrao.php and create a new file that can be sql / oficials.php, it will look like this:

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 the pasta.php to a new file, view / historical_list.php which will only have a foreach to list the historics

<?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 not we use functions of type mysql_ *?

MySQL vs PDO - Which is the most recommended to use?

    
20.05.2014 / 15:13
5

I particularly prefer using full names that facilitate debugging and maintenance by others. Example:

$queryFuncionarios 

$queryDiasPlantao

This makes it much easier

    
20.05.2014 / 14:52
1

Before running multiple queries you need to analyze whether they are independent queries or not.

Imagine, for example, an action log system that records all actions taken by a particular user.

If you run two queries at the same time, one to insert and one to register the inclusion by user X, if the first one fails and the second does not, you are inconsistent.

Of course, a system of logs that are often often clean is not a troubling problem, but it is a problem indeed.

Whereas if you attempt to insert and fail, the second query will not run, and you might still be able to handle the error by telling the user that that particular action failed, rather than a general error of "something went wrong. "

    
20.05.2014 / 18:24