Automation of registers (MySQL)

2

Hello, everyone!

I have a problem and I need your help to create a solution. My situation is this: There is a .php page that will be accessed daily by several people, this page will execute a .sql script and return a value ... Only that is where the problem is ...

I need to create a queue system, something like this, because .sql can not be run at the same time by multiple people.

Any ideas on how to proceed? Thank you in advance!

    
asked by anonymous 24.05.2015 / 23:47

1 answer

2

You need to create a table in the DB called queue with the values of the users and the wait field of value 1 and 0 in order of arrival.

Before the user uses his script he enters this table with a value of 0, if he is the first one in the list the value of it will be 1 and he will use the script. After use it has to be deleted from this table.

In addition the users that are on hold have to be informed of this in a page and this page has to have an auto refresh every 1 minute for example for a new check in the database

See the table:

queue

  

id,   email, wait

verify.php

    <?php 


    // verifica se existe algum usuário usando o sistema 
    $selecionaUsuariosUsandoSistema = mysql_query("SELECT * FROM fila WHERE espera = 1");

    // verifica se existe algum usuário na fila
    $selecionaUsuariosEspera = mysql_query("SELECT * FROM fila WHERE espera = 0");

    if(mysql_num_rows($selecionaUsuariosUsandoSistema) == 0 && mysql_num_rows($selecionaUsuariosEspera) == 0){

        // se não existir o usuario usando o sistema ou na fila, ele será inserido no banco de dados depois redirecionado direto para o sistema

        mysql_query("INSERT INTO fila(id, email, espera) VALUES (0, '$emailUsuario', 1)");

        header("Location: diretoParaOScript.php");

    } else {

        // se existir algum usuario na fila ou no sistema ele sera inserido no banco de dados com valor 0 e redirecionado para página espera.php

        mysql_query("INSERT INTO fila(id, email, espera) VALUES (0, '$emailUsuario', 0)");

        header("Location: espera.php");

    }

    ?>

wait.php

   <?php

$selecionaUsuariosEspera = mysql_query("SELECT email FROM fila ORDER BY id");
$usuariosEmEspera = mysql_fetch_array($selecionaUsuariosEspera);

$emailUsuario = '[email protected]';

//verifica se o email do usuário é o próximo da fila

if($emailUsuario == $usuariosEmEspera[0]){
    //se for o próximo, ele edita o valor do usuário para 1 e redireciona para o sistema
    mysql_query("UPDATE fila SET espera = '1' WHERE email = '$emailUsuario'");

    header("Location: diretoParaOScript.php");

} else {


   ?>
   <html>
   <head>
    <meta http-equiv="refresh" content="60">
    </head>
   <body>

aguarde...

    </body>
    </html>

   <?php } ?>

After the user closes the system it has to be deleted from the table

Other considerations ...

I created this method to give you a light because I do not know how your system works.

You have to consider some things like:

  • What if the user closes the browser before using the system, will the queue stop?
  • Maybe methods with cookies and other sessions are required to verify that the user is accessing the system, if it is still in the queue and discarding it if not.

Good luck

    
25.05.2015 / 01:32