title: Is there a better way to include() a PHP loop that depends on a varying MySQL query?
body: <p>I have a large PHP loop that I'm re-using in multiple spots on multiple pages of my site.  I'd normally just use an include() to set that PHP loop wherever it's needed, but the PHP loop in question makes use of a MySQL query in which I need to change variables (different filters and different LIMITs) for different instances of this PHP loop.</p>
  
  <pre><code>&lt;?php 
      $results = mysql_query("SELECT * FROM databaseName ORDER BY columnName DESC, otherColumnName DESC LIMIT 6", $connection);
      if (!$results) {
          die("Database Query Failed");
      }
  ?&gt;
  
  &lt;?php
      while ($variable = mysql_fetch_array($results)) {
          ----Execute this loop----
      };
  ?&gt;
  </code></pre>
  
  <p>Which I then recalled wherever I needed it using</p>
  
  <pre><code>&lt;?php include("/filepath/includedfile.php"); ?&gt;
  </code></pre>
  
  <p>Everything was running fine until I took the actual MySQL query OUT of the included file and placed it before each instance of that included PHP (so I could control certain variables for each instance of the query).  Now, the MySQL query and the included PHP loop don't seem to be communicating the same way, and I'm getting the following warning:</p>
  
  <blockquote>
    <p>"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/filepathhere/includedfile.php on line 3"&lt;</p>
  </blockquote>
  
  <p>I feel like the included PHP is being run in the included file and THEN being injected into the main page.  I need the code to be injected into the main page before it gets run.</p>
  
  <p>Is there a better way than "include()" to inject that PHP loop into the main page?</p>