How to enter a keyword in a search in the bank already done

0

For example, I did a search of all the user data in the bank to echo echoes in different columns through the site:

    $stmt = $conexao->prepare("SELECT * FROM esc_usuarios WHERE usu_codigo = ?");
    $stmt->bind_param("s", $usu_codigo);
    $stmt->execute();
    $usu_info_result = $stmt->get_result();
    $usu_info_coluna = $usu_info_result->fetch_assoc();
    $stmt->close();

Somewhere else on the site, I wanted to show this result, however, using an ORDER BY or LIMIT, such as:

    $stmt = $conexao->prepare("SELECT * FROM esc_usuarios WHERE usu_codigo = ? ORDER BY usu_datacadastro DESC LIMIT 5");

How can I do this without having to prepare another query, since I already have the result and just want to filter the output?

    
asked by anonymous 23.10.2018 / 20:24

1 answer

0

I do not have enough reputation to comment, so I ask you: can you confirm that all users have been stored as an array in the variable $ usu_info_column? And what is the structure of the data?

print_r($usu_info_coluna);

The normal is to use a while with fetch_assoc to retrieve each result by putting it in an array, like this:

function listaProdutos($conexao) {
    $produtos = array(); // aqui criamos um array vazio
    $resultado = mysqli_query($conexao, "select * from produtos"); // aqui selecionamos todos os produtos do nosso banco

    while($produto = mysqli_fetch_assoc($resultado)) { // aqui dizemos que enquanto houver produtos no nosso resultado realizaremos a logica a seguir
        array_push($produtos, $produto); // finalmente colocamos nosso produto dentro do array de produtos
}

But I believe your object is already performing this logic and bringing all results to the variable $ usu_info_column. Starting from this principle, let's solve it:

LIMIT 5 - > When publishing the results, simply print only the quantity you need:

for($i = 0; $i <= 5; $i++){
    echo "Data: " . $usu_info_coluna[$i][usu_datacadastro] . "Nome: " . $usu_info_coluna[$i][usu_nome];
}
    
24.10.2018 / 02:16