You tagged that question with the tags php and database. Although I didn’t use the mysql, I see in your example code you’re calling mysqli
, then I hope you are using some version of Mysql. If you are wrong, please tell us.
Not enough information has been provided to "guess" how you are tracking these searches. Or what your intention is with them. If you want to track each search individually (for example, generate a chronological log) I suggest you record this in a dedicated table and use a primary key id
with the guy AUTO_INCREMENT
. Perhaps it is also interesting one or two fields that store the timestamp of the query, so that you can have chronological traceability and not just numerical.
Maintain a column id
of the kind AUTO_INCREMENT
in primary key is a joker to solve most of the relational logics you may have. Allows you to search for older, newer ones, sort, group, or filter with ease. If you have date fields as auxiliaries, higher granularity still.
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 wish, however, store queries unique to your search system (ex: want to know you searched "fried chicken", but do not want an input for each query) you can still use the same field id
with AUTO_INCREMENT
as I mentioned, just check if 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);
As you would like to deal with dates (in case of dealing with dates), it is up to you. You can manually write the functions that suit you best or if you want a library that provides this type of help you can try the Carbon.
The truth is that, without more specific information, any answer will be speculative and its accuracy will be as subjective as the question itself. There are numerous 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 have a server sign to verify the authenticity and then return to your backend. Do you need to do this? Most likely nay. But only you can tell us.
I sincerely 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, when it comes to PHP, one of the problems of PHP code demonstrated above is the ease of a bad person to inject code into your database and cause all kinds of problems. There are native functions to prepare your query and as far as possible avoid a malicious code injection through your system.
I suggest you read on official PHP documentation about Mysqli before proceeding with your project.
It could better detail what the "number for each query" would be and describe why it needs it and how it will be used?
– Woss
@Andersoncarloswoss updated the question
– Henrique Paiva
Any specific need for random number? Wouldn’t an id with auto increment be better? For, however small the chance, certainly one hour the Rand will give a repeated number. In order not to take that risk, you would first need to consult if Rand does not exist in the bank and then record.
– Rafael