title: Fatal error: Call to undefined method mysqli::sql_query()
body: <p>I am using PHPBB3 3.0.12 and I have a script that does an auto-login as described here:</p>
  
  <p><a href="https://stackoverflow.com/questions/5092340/phpbb3-auto-login?noredirect=1&amp;lq=1">phpBB3 auto-login</a></p>
  
  <p>Here is my script</p>
  
  <pre><code>&lt;?php
  
  login();
  
  function login(){
  
    define('IN_PHPBB', true);
  
    $phpbb_root_path = pathNLP() . 'forum/'; //the path to your phpbb relative to this script
    $phpEx = substr(strrchr(__FILE__, '.'), 1);
    include($phpbb_root_path . "common.php"); ////the path to your phpbb relative to this script
    include($phpbb_root_path . "includes/functions_user.php");
  
    // Start session management
    $user-&gt;session_begin();
    $auth-&gt;acl($user-&gt;data);
    $user-&gt;setup();
  
    $username = "testuser"; $password = "1";
    if(isset($username) &amp;&amp; isset($password))
    {
      $result=$auth-&gt;login($username, $password, true);
  
      if ($result['status'] == LOGIN_SUCCESS) {
        echo "You're logged in";
      }
    }
  }
  </code></pre>
  
  <p>This used to work on my webhoster. I am trying this script currently on my local PC with XAMPP but I get the following error: </p>
  
  <pre><code>Fatal error: Call to undefined method mysqli::sql_query() in /home/adam/www/mysite/forum/includes/cache.php on line 51
  </code></pre>
  
  <p>and indeed, on line 51 in <code>cache.php</code> I find the command <code>sql_query</code>:</p>
  
  <pre><code>    global $db
      // ...
      $sql = 'SELECT config_name, config_value, is_dynamic
          FROM ' . CONFIG_TABLE;
      $result = $db-&gt;sql_query($sql); //&lt;--line 51
  </code></pre>
  
  <p>I think the problem is the <code>global $db</code> line, I guess <code>$db</code> should not be a mysqli object, since it does not have a <code>sql_query</code> method. According to a quote from <a href="https://www.phpbb.com/community/viewtopic.php?f=46&amp;t=1267665" rel="nofollow noreferrer">https://www.phpbb.com/community/viewtopic.php?f=46&amp;t=1267665</a> (Thanks to Patrik Q for the link) someone said:</p>
  
  <blockquote>
    <p>There is a design error in phpbb3. They use global $db EVERYWHERE
    instead of using a singleton for the db object. The problem in my case
    was that i was putting this code inside of a class. [...] in my case i
    just moved that code to outside the class and copied the vars in the
    constructor</p>
  </blockquote>
  
  <p>Because of this quote I tried to call the script not inside a function - and to my surprise it worked! However, I need to call it from a function. I do not understand why <code>global $db</code> makes problems when putting it in a function. What can I do to make the script work inside a function? Here is the working code:</p>
  
  <pre><code>&lt;?php
  
    define('IN_PHPBB', true);
  
    $phpbb_root_path = pathNLP() . 'forum/'; //the path to your phpbb relative to this script
    $phpEx = substr(strrchr(__FILE__, '.'), 1);
    include($phpbb_root_path . "common.php"); ////the path to your phpbb relative to this script
    include($phpbb_root_path . "includes/functions_user.php");
  
    // Start session management
    $user-&gt;session_begin();
    $auth-&gt;acl($user-&gt;data);
    $user-&gt;setup();
  
    $username = "testuser"; $password = "1";
    if(isset($username) &amp;&amp; isset($password))
    {
      $result=$auth-&gt;login($username, $password, true);
  
      if ($result['status'] == LOGIN_SUCCESS) {
        echo "You're logged in";
      }
    }
  </code></pre>
  
  <p>What can I do to make this script available in a method?</p>