Jump to content

Recommended Posts

I'm trying to optimize my site and the first thing on my list is my stats file.  This file contains all the queries that my site uses, but not all queries are used on every page.  For example, the page gets all the information for news posts, tutorials, roster, podcast...everything, but the news page only uses the news post query.

 

I have no way of telling the page to only run the news post query, meaning that everytime any page that includes this stats file is requested, it runs all queries.  This is obviously bad for multiple reason, the most obvious being server stress and page load times.  I'd like to figure out how to tell that page to only load the queries I want it to.

 

There are some queries there that every page does use, like number of posts and users, etc.  Those will have to always be run.  My idea was to simply put the ones that I wanted to control inside functions.  Right now I've got my news query as, say, this:

 

$news = mysql_query("[query]");

 

And on the news page, I use that by doing this:

 

while($info = mysql_fetch_assoc($news)) {
  [...]
  }

 

So I thought if I put the news query inside a function, like this:

 

function fetch_news() {
  $news = mysql_query("[query]");
  return $news;
  }

 

Then changed my code on my news page to this:

 

while($info = mysql_fetch_assoc(fetch_news())) {
  [...]
  }

 

That it would work.  Well, it doesn't.  The page thinks and thinks and thinks for a long time, then gives me a 500 Server Error.

 

What can I do to make those queries run from a function, or if a better way exists to make sure I'm only running them when I need them, how can I do that?

 

Thanks in advance.

 

EDIT: Actually, on further investigation, I get a mysql_error() message:

 

Fatal error: Cannot redeclare fetch_roster() (previously declared in [stats.php path]:24) in [stats.php path] on line 27

 

This was the function I was using, lines 24-27 of stats.php:

 

function fetch_roster() {
  GLOBAL $link;
  $roster = mysql_query('SELECT * FROM roster ORDER BY role=\'Peon\', role=\'Member\', role=\'Staff\', role=\'Overlord\'', $link) or die(mysql_error());
  return $roster;
  }

 

And my code to use it in roster.php:

 

while($info = mysql_fetch_assoc(fetch_roster())) {
  [...]
  }

 

EDIT Again: Nope, I was getting that error because I included header.php, which included stats.php, but I was also including stats.php in the roster.php file, so I was including stats.php twice.  I still have the issue of the page loading for a long time then giving me a 500 Server Error.

If I understand your question properly, because I would still have the problem of returning the result of that query back to my calling script.

 

However, I did figure this out.  I was having the issue because I was using this code:

 

while($info = mysql_fetch_assoc(fetch_news())) {
  [...]
  }

 

When I should have been doing this:

 

$news = fetch_news();

while($info = mysql_fetch_assoc($news)){
  [...]
  }

 

Changing that little bit made it work.

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.