musclehead Posted December 10, 2007 Share Posted December 10, 2007 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! Quote Link to comment Share on other sites More sharing options...
trq Posted December 10, 2007 Share Posted December 10, 2007 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; } } } ?> Quote Link to comment Share on other sites More sharing options...
musclehead Posted December 10, 2007 Author Share Posted December 10, 2007 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! Quote Link to comment Share on other sites More sharing options...
trq Posted December 10, 2007 Share Posted December 10, 2007 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. Quote Link to comment Share on other sites More sharing options...
musclehead Posted December 10, 2007 Author Share Posted December 10, 2007 Thorpe - thanks again. I guess I'll just deal with the lag...could be worse, I suppose! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.