Store query content in an array

Asked

Viewed 720 times

0

Hello, I have a class that performs a query in the database:

$itemObra->listarItensObra($_GET['id'], 0);

I need to use this query several times and I would like the query to occur only once and the items to be stored in an array, and then use a foreach to list.

foreach ($itemObra->listarItensObra($_GET['id'], 0) as $item) {
//....
}

Any idea is welcome!

2 answers

3


You have to see its architecture but theoretically if your listItensObra returns the array already does so:

$resultado = $itemObra->listarItensObra($_GET['id'], 0);

But it is not indicated to use parameters of the way you are using indico use so:

$id = mysql_real_escape_string($_GET['id']);
$resultado = $itemObra->listarItensObra($id, 0);
  • Thank you for answering, in case if I use a variable it is not just storing the query function? instead of storing the result in the query? Hug!

  • 1

    It will store whatever its function returns. Post the code of your list methodItensObra

1

If the query will occur several times and if the data of this query does not change easily you can also store in a session.

For example:

$_SESSION['OBRAS'] = $itemObra->listarItensObra($id, 0);

This allows you to use the $_SESSION code anywhere to do the listing.

Browser other questions tagged

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