Generate query number? [closed]

-4

I have a search system and I want to generate a number for each search done in the database, I'm thinking of using rand to generate the numbers, then get the generated number and save it in the database.

Would it be a good idea to use rand function for this?

Example:

include_once('conexao.php');

$nome = $_GET('nome');

$n = rand(0, 100000);

$cons = "INSERT INTO nconsulta (n1consultan, nome) VALUES '$n', 'nome'";
$executar = mysqli_query($conexao, $cons);
    
asked by anonymous 07.11.2018 / 11:35

3 answers

4

You have tagged this question with the tags and database-database . Although I did not use the , I see in your code sample which is calling mysqli , so I'm assuming you're using some version of MySQL. If you're wrong, please tell us.

Not enough information was provided to "guess" how you are tracking those searches. Or what their intention is with them. If you want to crawl each search individually (for example, generate a chronological log) I suggest you write this to a dedicated table and use a id primary key with type AUTO_INCREMENT . It may also be interesting to have one or two fields that store the timestamp of the query, so you can have chronological and not just numeric traceability.

Keeping a id column of type AUTO_INCREMENT in primary key is a wildcard to solve most of the relational logics you may have. It allows you to search the oldest, latest, sort, group or filter with ease. If you have the date fields as auxiliaries, you still have more granularity.

CREATE TABLE query_log (
     id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
     query_string TEXT NOT NULL,
     created_at DATETIME NOT NULL,
     updated_at DATETIME NOT NULL
   );

INSERT INTO query_log (query_string, created_at, updated_at)
    VALUES("frango frito", NOW(), NOW());

If you want to store unique queries to your search system (eg you want to know that you searched for "fried chicken" but do not want an entry for each query) the same field id with AUTO_INCREMENT as I mentioned, just make sure the query text already exists before saving.

Using your "example":

include_once('conexao.php');

$nome = $_GET('nome');      // Isso aqui deveria ser a "frase" da consulta???
// $n = rand(0, 100000);    // Não precisamos mais "gerar" o ID

$cons = "INSERT INTO query_log (query_string, created_at, updated_at) VALUES ('$nome', '2018-11-07 11:21:00', '2018-11-07 11:21:00')";
$executar = mysqli_query($conexao, $cons);

How you would like to handle dates (in the case of dealing with dates) is at your discretion. You can manually type the functions that suit you best, or if you want a library that provides this kind of help you can try Carbon . p>

The truth is that without more specific information, any response will be speculative and its accuracy will be as subjective as the question itself. There are countless ways to implement what you have written, right or wrong depends on what you want; which is exactly what is unclear.

You could generate a hash of the query, encrypt the results and make a XOR then invert the bits and send them to a server to verify authenticity and then return to your backend. Need to do this? Most likely not . But only you can tell us.

I honestly believe that, in this case, using date literals or UID functions are overkill, unnecessary or will bring other problems in the future.

By the way, for PHP, one of the problems of PHP code shown above is the ease of a malicious person to inject code into your database and cause all sorts of problems. There are native functions to prepare your query and as far as possible avoid an injection of malicious code through your system.

I suggest you read the official MySQLi PHP documentation before continue with your project.

    
07.11.2018 / 14:04
0

You can pick up the current time and multiply with some random value, or even the last id of your bank.

    $id=10;
    $data = date('Y/m/d H:i:s');
    $x = $id*strtotime($data);
    
07.11.2018 / 12:14
0

I believe that if you can better explain your need for the random number someone can show you a better way.

At first, if there is no reason for this number to be random, MySQL has the "auto-increment" field, which will generate a different number for each line. ( See more here. )

If this does not meet your need, you can develop a logic to generate a random ID, based on the current time among other things. Something like this here:

$uid = hexdec(uniqid());
// exemplo de saída: 1616475946956388

Remembering that uniqid returns numbers in their hexadecimal representation, so it is necessary to convert them to only numbers, as in the above code.

You can also add the current date (unix) to make it even more unique:

$uid = time() . '' . hexdec(uniqid());
// exemplo de saída: 15415914151616475760156156

So you will not have to query the database before entering a line to check if the ID already exists.

    
07.11.2018 / 12:50