title: Slim Framework - How to query db in normal function
body: <p>I'm very new to PHP and Slim Framework which helps creating APIs. 
  Everything is ok If i query db inside <code>$app-&gt;post</code> or <code>get</code>. But I want to separate it to normal function. It will help when I need to use it later in other APIs.</p>
  
  <p>I tried to call this</p>
  
  <pre><code>$app-&gt;get('/search/[{phone}]', function($request, $response, $args) use ($app){
      $token = $response-&gt;getHeader('token');
      // $phone = $args['phone'];
      if (isTokenValid($token)){
          return $this-&gt;response-&gt;withJson("valid");
      }
      return $this-&gt;response-&gt;withJson("invalid");
  
  });
  </code></pre>
  
  <p>My <code>isTokenValid()</code> function</p>
  
  <pre><code>function isTokenValid($token){
      $sql = 'SELECT id FROM users WHERE token = :token';
      $s = $app-&gt;db-&gt;prepare($sql); //&lt;&lt; this line 25
      $s-&gt;bindParam(':token', $token);
      if ($s-&gt;execute()){
          if($sth-&gt;rowCount() &gt; 0){
              return true;
          }
      }
      return false;
  }
  </code></pre>
  
  <p>But I get <code>500 Internal Server Error</code></p>
  
  <pre><code>Type: Error
  Message: Call to a member function prepare() on null
  File: /Applications/MAMP/htdocs/aigoido/src/functions.php
  Line: 25
  </code></pre>
  
  <p>How to call it outside <code>$app</code>? Thanks.</p>