Jump to content

[SOLVED] Storing MySQL Query Results in PHP Array


musclehead

Recommended Posts

I want to store the results of a query:

 

$query = mysql_query("SELECT * FROM table");

 

in an array in PHP in order to access and sort the array as opposed to re-querying the table each time I want to sort or view results in some way.

 

How can I best achieve this? Thank you!

Its best to do all your sorting in your query, but anyway....

 

<?php

  $array = array();
  if ($result = mysql_query("SELECT * FROM table")) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        $array[] = $row;
      }
    }
  }

?>

Its best to do all your sorting in your query, but anyway....

 

Thorpe - that's exactly my reason for asking - I have a table with about 3000 records displayed on a page. Once displayed, I want to sort the results by various column headings, which I'm doing successfully by executing a new query w/ an ORDER BY and sort clauses, but that takes some time - I'm trying to get everything running quicker. Any idea? And by the way, thanks!

Theres not really any better way of doing it. The script is still going to need to be executed again to refresh the order. Therefore you query will need to be executed again anyway.

 

Its always more efficient to get the data from the database in the order you want it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.