Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.