title: Call to a member function real_escape_string() on a non-object
body: <p>I'm getting this error: </p>
  
  <blockquote>
    <p>Call to a member function
    real_escape_string() on a non-object</p>
  </blockquote>
  
  <p>and here's <code>$db</code></p>
  
  <blockquote>
    <p>$db = new mysqli("127.0.0.1", "username", "password", "sch5400");</p>
  </blockquote>
  
  <p>in code:</p>
  
  <pre><code>function readSession($sessionId) {
      global $db;
      $sessionId = session_id();
      // escape session ID
      $sessionId = $db-&gt;real_escape_string($sessionId);
      $time = time();
      $result = $db-&gt;query("SELECT sessiondata FROM sessions WHERE sessionid='$sessionId' AND expiry &gt; $time");
      if ($result-&gt;num_rows &gt; 0) {
          $row = $result-&gt;fetchRow();
          return $row['sessiondata'];
      }
      // return empty string
      return "";
      }
  </code></pre>
  
  <p>It appears on line 5, relative to code above. Didn't I instantiate <code>$db</code>?</p>