Automation of registries (Mysql)

Asked

Viewed 478 times

2

Hello, everybody!

I have a problem, and I need your help to come up with a solution. My situation is as follows: There is a page . php that will be accessed daily by several people, this page will run a script. sql and return a value... Only that’s where the problem is...

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

Any idea how to proceed? Thank you in advance!

1 answer

2


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

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

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

See the table:

queue

id, email, hold on

check.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");

    }

    ?>

php hold.

   <?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 user close system it has to be deleted from table

Other considerations...

I created this method to give you a light because I don’t know how your system works.

you have to consider some things like:

  • And if the user closes the browser before using the system, it will stop the queue?
  • maybe methods with cookies and other Sesssions are necessary to check if the user is accessing the system, if he is still in the queue and discard it if not.

good luck

  • I got Andrei, I thought your solution was really good, the idea of creating a db for the queue and everything. Actually I’m just analyzing the same logic, I haven’t decided in which language I will make the code. Thank you very much! That’s what I needed ;)

  • Quiet @Wesley, don’t forget to approve the answer if it helped you. = ) hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.