title: Warning: mysqli_query(): Couldn't fetch mysqli
body: <p>I have a problem where I can not retrieve the result from my MySQL database (via PHP). I use the same function in other places and it works flawlessly. However at this point i keep getting the "Warning: mysqli_query(): Couldn't fetch mysqli" error. Details of the problem are explained below.
  I use a quite similar function elsewhere (getAllCountries as seen below) in my PHP which does work perfectly:</p>
  
  <pre><code>function getAllCountries()
  {
      $result = db_query("SELECT countryid, name FROM country ORDER BY name ASC");
  
      echo "&lt;select class=addresscountry name=country&gt;";
      while($row = mysqli_fetch_array($result)) {
        echo '&lt;option value="' . $row['countryid'] . '"&gt;' . $row['name'] . '&lt;/option&gt;';
      }
      echo "&lt;/select&gt;";
  
      mysqli_close(db_connect());
  }
  </code></pre>
  
  <p>So the problem is the following: </p>
  
  <p>I have a php file containing the following code:</p>
  
  <pre><code>&lt;?php
  require 'includes/functions.php';
  
  function getUserPicPath()
  {
      $userid = $_SESSION['userid'];
  
      $result = db_query("SELECT picture FROM user WHERE userid='$userid'");
  
      while($row = mysqli_fetch_array($result)) {
          $picturepath = $row['picture'];
      }
  
      echo $picturepath;
  
      mysqli_close(db_connect());
  }
  </code></pre>
  
  <p>my functions.php file has the following line (together with other non-relevant functions):</p>
  
  <pre><code>require 'dbfunctions.php';
  </code></pre>
  
  <p>and my dbfunctions.php looks like this:</p>
  
  <pre><code>&lt;?php
  function db_connect()
  {
      require ".db_password.php";
  
      static $connection;
  
      if(!isset($connection)) {
          $connection = mysqli_connect('localhost',$username,$password,$dbname);
      }
  
      if($connection === false) {
          return mysqli_connect_error(); 
      }
  
      return $connection;
  }
  
  function db_query($query) 
  {
      $connection = db_connect();
  
      $result = mysqli_query($connection,$query);
  
      return $result;
  }
  </code></pre>
  
  <p>In my PHP document I call the following function :</p>
  
  <pre><code>if ($userid == -1)
      {
          showNotAuthorizedPage();
      } else {
          myAccountPage();
      }
  </code></pre>
  
  <p>and the myAccountPage() function is declared in the same file as the getUserPicPath() function, this getUserPicPath() function is called as follows:</p>
  
  <pre><code>&lt;div id="tabs-2"&gt;
      &lt;p&gt;&lt;?php getUserPicPath(); ?&gt;&lt;/p&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p>I use the tabs (<a href="http://jqueryui.com/tabs/#default">http://jqueryui.com/tabs/#default</a>) on my webpage and that is where i want to call it in.
  The myAccountPage() function which gives the following error :</p>
  
  <pre><code>Warning: mysqli_query(): Couldn't fetch mysqli in C:\Users\Dennis\Documents\My Dropbox\xxx\zzz\www\Project Files\includes\dbfunctions.php on line 29
  Call Stack
  #   Time    Memory  Function    Location
  1   0.0000  256880  {main}( )   ..\myaccount.php:0
  2   0.0010  283328  myAccountPage( )    ..\myaccount.php:181
  3   0.0070  285368  getUserPicPath( )   ..\myaccount.php:121
  4   0.0070  285528  db_query( ) ..\myaccount.php:11
  5   0.0070  285624  mysqli_query ( )    ..\dbfunctions.php:29
  
  ( ! ) Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\Users\Dennis\Documents\My Dropbox\me&amp;roxy\WE\final project\Project Files\myaccount.php on line 13
  Call Stack
  #   Time    Memory  Function    Location
  1   0.0000  256880  {main}( )   ..\myaccount.php:0
  2   0.0010  283328  myAccountPage( )    ..\myaccount.php:181
  3   0.0070  285368  getUserPicPath( )   ..\myaccount.php:121
  4   0.0080  285768  mysqli_fetch_array ( )  ..\myaccount.php:13
  
  ( ! ) Notice: Undefined variable: picturepath in C:\Users\Dennis\Documents\My Dropbox\me&amp;roxy\WE\final project\Project Files\myaccount.php on line 17
  Call Stack
  #   Time    Memory  Function    Location
  1   0.0000  256880  {main}( )   ..\myaccount.php:0
  2   0.0010  283328  myAccountPage( )    ..\myaccount.php:181
  3   0.0070  285368  getUserPicPath( )   ..\myaccount.php:121
  
  ( ! ) Warning: mysqli_close(): Couldn't fetch mysqli in C:\Users\Dennis\Documents\My Dropbox\me&amp;roxy\WE\final project\Project Files\myaccount.php on line 19
  Call Stack
  #   Time    Memory  Function    Location
  1   0.0000  256880  {main}( )   ..\myaccount.php:0
  2   0.0010  283328  myAccountPage( )    ..\myaccount.php:181
  3   0.0070  285368  getUserPicPath( )   ..\myaccount.php:121
  4   0.0100  285864  mysqli_close ( )    ..\myaccount.php:19
  </code></pre>