HaLo2FrEeEk Posted March 10, 2010 Share Posted March 10, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194770-calling-mysql_query-fron-inside-a-function/ Share on other sites More sharing options...
Wolphie Posted March 10, 2010 Share Posted March 10, 2010 Why not put your queries inside an associative array and then run the mysql_function() on a specific query inside the script that it should be ran? Quote Link to comment https://forums.phpfreaks.com/topic/194770-calling-mysql_query-fron-inside-a-function/#findComment-1024205 Share on other sites More sharing options...
HaLo2FrEeEk Posted March 10, 2010 Author Share Posted March 10, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194770-calling-mysql_query-fron-inside-a-function/#findComment-1024235 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.