title: Putting a SQL query in a PHP function
body: <p>I've got several queries I want to run on a single page. I obviously don't want to put the actual queries in my template file, so I <em>think</em> what I want to do is construct a function and call it wherever I want the query results to show up. Right? </p>
  
  <p>So, for example,  I'll have <code>&lt;?php sidebar_query()?&gt;</code> in the sidebar, <code>&lt;?php footer_query()?&gt;</code> in the footer, etc. </p>
  
  <p>So, I just make a file called <code>functions.php</code>, do PHP include, and put something like this in there?</p>
  
  <pre><code>&lt;?php
      function sidebar_query(){
          $query = ("SELECT sidebarposts FROM table;");
      return $query;
  }
  ?&gt;
  </code></pre>
  
  <p>or do you use <code>echo</code> and not return anything?</p>
  
  <pre><code>&lt;?php
      function sidebar_query(){
          $query = ("SELECT sidebarposts FROM table;");
          echo $query;
  }
  ?&gt;
  </code></pre>
  
  <p>Along the exact same line, I'd like to count the results that get returned, and display a 'There were X posts returned!' message below. Seems like it would make sense to put this in a function too. How would I make a 'generic' function that I could reuse for each query? </p>
  
  <pre><code>&lt;?php
      function number_of_results(){
          $num_rows = mysql_num_rows();
          echo $num_rows;
  }
  ?&gt;
  </code></pre>
  
  <p>I'd be extremely grateful if someone could give me the theoretical gist of what I should be trying to achieve here.  </p>
  
  <p>Thanks for helping a beginner.</p>
  
  <p>Terry</p>