Craig Jones
Craig Jones

Reputation: 15

What is the most efficent way to populate a table using PHP?

I'm using a jQuery script for a table which paginates and searches through data. It works and functions as it is supposed to.

I'm populating the table with data which consists of over 1,000 records and I'm currently using a PHP while statement to populate it.

Within the table code I have this statement, first I execute the query to get the data, $article = mysql_query("SELECT * FROM articles ORDER BY published DESC");

Then I use the while statement to populate - while($articles = mysql_fetch_array($article)) {

Whilst this does work, the page load time is so poor and takes a long time to load. Obviously this isn't the most effective way of populating. Is there an 'easier' and efficent way of doing this?

Upvotes: 0

Views: 114

Answers (3)

Jack Albright
Jack Albright

Reputation: 509

Are you showing all 1000 articles on one page or are you using jQuery to page through sets of, say, 50 at a time?

If you are doing the latter, why are you getting all 1000 records? Instead you could change your sql to something like this:

 $startRecord = N // set to some value, initially 0
 $numRecords = 50;

 $query = "SELECT * FROM articles ORDER BY published DESC limit $startRecord, $numRecords";
 $result = mysql_query($query);

This would get the first 50 records. Modify the $startRecord var to get other records from your data set.

Upvotes: 1

Vitalii Plodistov
Vitalii Plodistov

Reputation: 125

(mysql)OFFSET + (php)PAGINATION + (mysql)OPTIMIZATION (such as Index key...)

Upvotes: 0

Max Hudson
Max Hudson

Reputation: 10226

You can change your data types to be smaller so you are downloading less though.

You probably have a lot of excess data you are downloading by selecting * instead of the specific tables you need as well.

You can also use LIMIT x to limit the number of articles/results you get (you probably don't actually need 1000 on a page right?..)

Other than that, there isn't much you can do to speed it up because it is taking so long to load due to the time it takes to download from the database, not the efficiency of the PHP script.

Upvotes: 1

Related Questions