How to insert a keyword into a database search already performed

Asked

Viewed 41 times

0

For example, I did a search of all user data in the database to echo in different columns by 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();

Elsewhere on the site, I wanted to show this result though, 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?

  • 1

    There’s no way to call result on the page you want and take from it just what you want?

  • @Magichat So, the part of picking up specific columns I know how to do, but filter with an order by or limit is that I don’t know the syntax.

  • Um... it’s a specific question of sql then?

  • I believe so...

  • But it is giving error this your query?

  • No, the query works perfectly, the problem is that I want to display the results of the first query elsewhere, without having to prepare another query, since the data I want, have already been searched in the first query, I just want to format with an order by

  • Type, you want to export the stored value from one variable to another page, e.g.: does the query on one page and pulls these values on another page?

  • that, I want to pull the query value, but sort the result, without having to create another query

  • I believe that you will store the returned data on select initial in a array.. then you can use some of those functions

  • So I gave a read on these functions, but I believe I’ve missed the syntax several times because I couldn’t apply in my code, can you give me an example?

Show 5 more comments

1 answer

0

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

print_r($usu_info_coluna);

The normal is to use a while with fetch_assoc to retrieve each result by placing it in an array, thus:

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 that your object is already performing this logic and taking all the results to the $usu_info_column variable. On this basis, we will come to the solution:

LIMIT 5 -> When publishing the results, just print out only the amount you need:

for($i = 0; $i <= 5; $i++){
    echo "Data: " . $usu_info_coluna[$i][usu_datacadastro] . "Nome: " . $usu_info_coluna[$i][usu_nome];
}
  • Yes, the object is already in the array, but I can’t get past the print part, it returns me Undefines offset, following your example, only the names are correct

  • I was able to limit using while, but how would the syntax be to sort the results by some column?

  • You need to use a PHP sorting function in an array prepared with its data, since the sorting is done by column. Put in the question the structure of the variable with the results that I put the code for you.

Browser other questions tagged

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