title: Fatal error: Call to a member function bind_param() on a non-object in /home/mysitename/public_html/members/models/funcs.php on line 382
body: <p>I am getting an error while creating member system for my website and the following code is of file named <code>funcs.php</code> but when I am trying to run any of the file whether it is <code>register.php or login.php</code>, it is showing me the following error:-</p>
  
  <pre><code> Fatal error: Call to a member function bind_param() on a non-object in /home/mywebsitename/public_html/members/models/funcs.php on line 382
  </code></pre>
  
  <p>Line 382 is:-   </p>
  
  <pre><code>$stmt-&gt;bind_param("is", $loggedInUser-&gt;user_id, $loggedInUser-&gt;hash_pw);
  </code></pre>
  
  <p>php coding for funcs.php file is as follows:-</p>
  
  <pre><code>&lt;?php    
  function getLanguageFiles()
  {
      $directory = "models/languages/";
      $languages = glob($directory . "*.php");
      //print each file name
      return $languages;
  }
  
  
  {
      $directory = "models/site-templates/";
      $languages = glob($directory . "*.css");
      //print each file name
      return $languages;
  }
  
  
  function getPageFiles()
  {
      $directory = "";
      $pages = glob($directory . "*.php");
      //print each file name
      foreach ($pages as $page){
          $row[$page] = $page;
      }
      return $row;
  }
  
  
  function destroySession($name)
  {
      if(isset($_SESSION[$name]))
      {
          $_SESSION[$name] = NULL;
          unset($_SESSION[$name]);
      }
  }
  
  
  function getUniqueCode($length = "")
  {   
      $code = md5(uniqid(rand(), true));
      if ($length != "") return substr($code, 0, $length);
      else return $code;
  }
  
  
  function generateActivationToken($gen = null)
  {
      do
      {
          $gen = md5(uniqid(mt_rand(), false));
      }
      while(validateActivationToken($gen));
      return $gen;
  }
  
  
  function generateHash($plainText, $salt = null)
  {
      if ($salt === null)
      {
          $salt = substr(md5(uniqid(rand(), true)), 0, 25);
      }
      else
      {
          $salt = substr($salt, 0, 25);
      }
  
      return $salt . sha1($salt . $plainText);
  }
  
  
  function isValidEmail($email)
  {
      if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
          return true;
      }
      else {
          return false;
      }
  }
  
  
  function lang($key,$markers = NULL)
  {
      global $lang;
      if($markers == NULL)
      {
          $str = $lang[$key];
      }
      else
      {
  
          $str = $lang[$key];
          $iteration = 1;
          foreach($markers as $marker)
          {
              $str = str_replace("%m".$iteration."%",$marker,$str);
              $iteration++;
          }
      }
  
      if($str == "")
      {
          return ("No language key found");
      }
      else
      {
          return $str;
      }
  }
  
  
  function minMaxRange($min, $max, $what)
  {
      if(strlen(trim($what)) &lt; $min)
          return true;
      else if(strlen(trim($what)) &gt; $max)
          return true;
      else
      return false;
  }
  
  
  function replaceDefaultHook($str)
  {
      global $default_hooks,$default_replace; 
      return (str_replace($default_hooks,$default_replace,$str));
  }
  
  
  function resultBlock($errors,$successes){
  
      if(count($errors) &gt; 0)
      {
          echo "&lt;div id='error'&gt;
          &lt;a href='#' onclick=\"showHide('error');\"&gt;[X]&lt;/a&gt;
          &lt;ul&gt;";
          foreach($errors as $error)
          {
              echo "&lt;li&gt;".$error."&lt;/li&gt;";
          }
          echo "&lt;/ul&gt;";
          echo "&lt;/div&gt;";
      }
  
      if(count($successes) &gt; 0)
      {
          echo "&lt;div id='success'&gt;
          &lt;a href='#' onclick=\"showHide('success');\"&gt;[X]&lt;/a&gt;
          &lt;ul&gt;";
          foreach($successes as $success)
          {
              echo "&lt;li&gt;".$success."&lt;/li&gt;";
          }
          echo "&lt;/ul&gt;";
          echo "&lt;/div&gt;";
      }
  }
  
  
  function sanitize($str)
  {
      return strtolower(strip_tags(trim(($str))));
  }
  
  
  
  
  function deleteUsers($users) {
      global $mysqli,$db_table_prefix; 
      $i = 0;
      $stmt = $mysqli-&gt;prepare("DELETE FROM ".$db_table_prefix."users 
          WHERE id = ?");
      $stmt2 = $mysqli-&gt;prepare("DELETE FROM ".$db_table_prefix."user_permission_matches 
          WHERE user_id = ?");
      foreach($users as $id){
          $stmt-&gt;bind_param("i", $id);
          $stmt-&gt;execute();
          $stmt2-&gt;bind_param("i", $id);
          $stmt2-&gt;execute();
          $i++;
      }
      $stmt-&gt;close();
      $stmt2-&gt;close();
      return $i;
  }
  
  
  function displayNameExists($displayname)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("SELECT active
          FROM ".$db_table_prefix."users
          WHERE
          display_name = ?
          LIMIT 1");
      $stmt-&gt;bind_param("s", $displayname);   
      $stmt-&gt;execute();
      $stmt-&gt;store_result();
      $num_returns = $stmt-&gt;num_rows;
      $stmt-&gt;close();
  
      if ($num_returns &gt; 0)
      {
          return true;
      }
      else
      {
          return false;   
      }
  }
  
  
  function emailExists($email)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("SELECT active
          FROM ".$db_table_prefix."users
          WHERE
          email = ?
          LIMIT 1");
      $stmt-&gt;bind_param("s", $email); 
      $stmt-&gt;execute();
      $stmt-&gt;store_result();
      $num_returns = $stmt-&gt;num_rows;
      $stmt-&gt;close();
  
      if ($num_returns &gt; 0)
      {
          return true;
      }
      else
      {
          return false;   
      }
  }
  
  
  function emailUsernameLinked($email,$username)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("SELECT active
          FROM ".$db_table_prefix."users
          WHERE user_name = ?
          AND
          email = ?
          LIMIT 1
          ");
      $stmt-&gt;bind_param("ss", $username, $email); 
      $stmt-&gt;execute();
      $stmt-&gt;store_result();
      $num_returns = $stmt-&gt;num_rows;
      $stmt-&gt;close();
  
      if ($num_returns &gt; 0)
      {
          return true;
      }
      else
      {
          return false;   
      }
  }
  
  
  function fetchAllUsers()
  {
      global $mysqli,$db_table_prefix; 
      $stmt = $mysqli-&gt;prepare("SELECT 
          id,
          user_name,
          display_name,
          password,
          email,
          activation_token,
          last_activation_request,
          lost_password_request,
          active,
          title,
          sign_up_stamp,
          last_sign_in_stamp
          FROM ".$db_table_prefix."users");
      $stmt-&gt;execute();
      $stmt-&gt;bind_result($id, $user, $display, $password, $email, $token, $activationRequest, $passwordRequest, $active, $title, $signUp, $signIn);
  
      while ($stmt-&gt;fetch()){
          $row[] = array('id' =&gt; $id, 'user_name' =&gt; $user, 'display_name' =&gt; $display, 'password' =&gt; $password, 'email' =&gt; $email, 'activation_token' =&gt; $token, 'last_activation_request' =&gt; $activationRequest, 'lost_password_request' =&gt; $passwordRequest, 'active' =&gt; $active, 'title' =&gt; $title, 'sign_up_stamp' =&gt; $signUp, 'last_sign_in_stamp' =&gt; $signIn);
      }
      $stmt-&gt;close();
      return ($row);
  }
  
  
  function fetchUserDetails($username=NULL,$token=NULL, $id=NULL)
  {
      if($username!=NULL) {
          $column = "user_name";
          $data = $username;
      }
      elseif($token!=NULL) {
          $column = "activation_token";
          $data = $token;
      }
      elseif($id!=NULL) {
          $column = "id";
          $data = $id;
      }
      global $mysqli,$db_table_prefix; 
      $stmt = $mysqli-&gt;prepare("SELECT 
          id,
          user_name,
          display_name,
          password,
          email,
          activation_token,
          last_activation_request,
          lost_password_request,
          active,
          title,
          sign_up_stamp,
          last_sign_in_stamp
          FROM ".$db_table_prefix."users
          WHERE
          $column = ?
          LIMIT 1");
          $stmt-&gt;bind_param("s", $data);
  
      $stmt-&gt;execute();
      $stmt-&gt;bind_result($id, $user, $display, $password, $email, $token, $activationRequest, $passwordRequest, $active, $title, $signUp, $signIn);
      while ($stmt-&gt;fetch()){
          $row = array('id' =&gt; $id, 'user_name' =&gt; $user, 'display_name' =&gt; $display, 'password' =&gt; $password, 'email' =&gt; $email, 'activation_token' =&gt; $token, 'last_activation_request' =&gt; $activationRequest, 'lost_password_request' =&gt; $passwordRequest, 'active' =&gt; $active, 'title' =&gt; $title, 'sign_up_stamp' =&gt; $signUp, 'last_sign_in_stamp' =&gt; $signIn);
      }
      $stmt-&gt;close();
      return ($row);
  }
  
  
  function flagLostPasswordRequest($username,$value)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("UPDATE ".$db_table_prefix."users
          SET lost_password_request = ?
          WHERE
          user_name = ?
          LIMIT 1
          ");
      $stmt-&gt;bind_param("ss", $value, $username);
      $result = $stmt-&gt;execute();
      $stmt-&gt;close();
      return $result;
  }
  
  
  function isUserLoggedIn()
  {
      global $loggedInUser,$mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("SELECT 
          id,
          password
          FROM ".$db_table_prefix."users
          WHERE
          id = ?
          AND 
          password = ? 
          AND
          active = 1
          LIMIT 1");
      $stmt-&gt;bind_param("is", $loggedInUser-&gt;user_id, $loggedInUser-&gt;hash_pw);    
      $stmt-&gt;execute();
      $stmt-&gt;store_result();
      $num_returns = $stmt-&gt;num_rows;
      $stmt-&gt;close();
  
      if($loggedInUser == NULL)
      {
          return false;
      }
      else
      {
          if ($num_returns &gt; 0)
          {
              return true;
          }
          else
          {
              destroySession("userCakeUser");
              return false;   
          }
      }
  }
  
  
  function setUserActive($token)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("UPDATE ".$db_table_prefix."users
          SET active = 1
          WHERE
          activation_token = ?
          LIMIT 1");
      $stmt-&gt;bind_param("s", $token);
      $result = $stmt-&gt;execute();
      $stmt-&gt;close(); 
      return $result;
  }
  
  
  function updateDisplayName($id, $display)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("UPDATE ".$db_table_prefix."users
          SET display_name = ?
          WHERE
          id = ?
          LIMIT 1");
      $stmt-&gt;bind_param("si", $display, $id);
      $result = $stmt-&gt;execute();
      $stmt-&gt;close();
      return $result;
  }
  
  
  function updateEmail($id, $email)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("UPDATE ".$db_table_prefix."users
          SET 
          email = ?
          WHERE
          id = ?");
      $stmt-&gt;bind_param("si", $email, $id);
      $result = $stmt-&gt;execute();
      $stmt-&gt;close(); 
      return $result;
  }
  
  
  function updateLastActivationRequest($new_activation_token,$username,$email)
  {
      global $mysqli,$db_table_prefix;    
      $stmt = $mysqli-&gt;prepare("UPDATE ".$db_table_prefix."users
          SET activation_token = ?,
          last_activation_request = ?
          WHERE email = ?
          AND
          user_name = ?");
      $stmt-&gt;bind_param("ssss", $new_activation_token, time(), $email, $username);
      $result = $stmt-&gt;execute();
      $stmt-&gt;close(); 
      return $result;
  }
  
  
  function updatePasswordFromToken($pass,$token)
  {
      global $mysqli,$db_table_prefix;
      $new_activation_token = generateActivationToken();
      $stmt = $mysqli-&gt;prepare("UPDATE ".$db_table_prefix."users
          SET password = ?,
          activation_token = ?
          WHERE
          activation_token = ?");
      $stmt-&gt;bind_param("sss", $pass, $new_activation_token, $token);
      $result = $stmt-&gt;execute();
      $stmt-&gt;close(); 
      return $result;
  }
  
  
  function updateTitle($id, $title)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("UPDATE ".$db_table_prefix."users
          SET 
          title = ?
          WHERE
          id = ?");
      $stmt-&gt;bind_param("si", $title, $id);
      $result = $stmt-&gt;execute();
      $stmt-&gt;close(); 
      return $result; 
  }
  
  
  function userIdExists($id)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("SELECT active
          FROM ".$db_table_prefix."users
          WHERE
          id = ?
          LIMIT 1");
      $stmt-&gt;bind_param("i", $id);    
      $stmt-&gt;execute();
      $stmt-&gt;store_result();
      $num_returns = $stmt-&gt;num_rows;
      $stmt-&gt;close();
  
      if ($num_returns &gt; 0)
      {
          return true;
      }
      else
      {
          return false;   
      }
  }
  
  
  function usernameExists($username)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("SELECT active
          FROM ".$db_table_prefix."users
          WHERE
          user_name = ?
          LIMIT 1");
      $stmt-&gt;bind_param("s", $username);  
      $stmt-&gt;execute();
      $stmt-&gt;store_result();
      $num_returns = $stmt-&gt;num_rows;
      $stmt-&gt;close();
  
      if ($num_returns &gt; 0)
      {
          return true;
      }
      else
      {
          return false;   
      }
  }
  
  
  function validateActivationToken($token,$lostpass=NULL)
  {
      global $mysqli,$db_table_prefix;
      if($lostpass == NULL) 
      {   
          $stmt = $mysqli-&gt;prepare("SELECT active
              FROM ".$db_table_prefix."users
              WHERE active = 0
              AND
              activation_token = ?
              LIMIT 1");
      }
      else 
      {
          $stmt = $mysqli-&gt;prepare("SELECT active
              FROM ".$db_table_prefix."users
              WHERE active = 1
              AND
              activation_token = ?
              AND
              lost_password_request = 1 
              LIMIT 1");
      }
      $stmt-&gt;bind_param("s", $token);
      $stmt-&gt;execute();
      $stmt-&gt;store_result();
          $num_returns = $stmt-&gt;num_rows;
      $stmt-&gt;close();
  
      if ($num_returns &gt; 0)
      {
          return true;
      }
      else
      {
          return false;   
      }
  }
  
  
  function createPermission($permission) {
      global $mysqli,$db_table_prefix; 
      $stmt = $mysqli-&gt;prepare("INSERT INTO ".$db_table_prefix."permissions (
          name
          )
          VALUES (
          ?
          )");
      $stmt-&gt;bind_param("s", $permission);
      $result = $stmt-&gt;execute();
      $stmt-&gt;close(); 
      return $result;
  }
  
  function deletePermission($permission) {
      global $mysqli,$db_table_prefix,$errors; 
      $i = 0;
      $stmt = $mysqli-&gt;prepare("DELETE FROM ".$db_table_prefix."permissions 
          WHERE id = ?");
      $stmt2 = $mysqli-&gt;prepare("DELETE FROM ".$db_table_prefix."user_permission_matches 
          WHERE permission_id = ?");
      $stmt3 = $mysqli-&gt;prepare("DELETE FROM ".$db_table_prefix."permission_page_matches 
          WHERE permission_id = ?");
      foreach($permission as $id){
          if ($id == 1){
              $errors[] = lang("CANNOT_DELETE_NEWUSERS");
          }
          elseif ($id == 2){
              $errors[] = lang("CANNOT_DELETE_ADMIN");
          }
          else{
              $stmt-&gt;bind_param("i", $id);
              $stmt-&gt;execute();
              $stmt2-&gt;bind_param("i", $id);
              $stmt2-&gt;execute();
              $stmt3-&gt;bind_param("i", $id);
              $stmt3-&gt;execute();
              $i++;
          }
      }
      $stmt-&gt;close();
      $stmt2-&gt;close();
      $stmt3-&gt;close();
      return $i;
  }
  
  
  function fetchAllPermissions()
  {
      global $mysqli,$db_table_prefix; 
      $stmt = $mysqli-&gt;prepare("SELECT 
          id,
          name
          FROM ".$db_table_prefix."permissions");
      $stmt-&gt;execute();
      $stmt-&gt;bind_result($id, $name);
      while ($stmt-&gt;fetch()){
          $row[] = array('id' =&gt; $id, 'name' =&gt; $name);
      }
      $stmt-&gt;close();
      return ($row);
  }
  
  
  function fetchPermissionDetails($id)
  {
      global $mysqli,$db_table_prefix; 
      $stmt = $mysqli-&gt;prepare("SELECT 
          id,
          name
          FROM ".$db_table_prefix."permissions
          WHERE
          id = ?
          LIMIT 1");
      $stmt-&gt;bind_param("i", $id);
      $stmt-&gt;execute();
      $stmt-&gt;bind_result($id, $name);
      while ($stmt-&gt;fetch()){
          $row = array('id' =&gt; $id, 'name' =&gt; $name);
      }
      $stmt-&gt;close();
      return ($row);
  }
  
  
  function permissionIdExists($id)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("SELECT id
          FROM ".$db_table_prefix."permissions
          WHERE
          id = ?
          LIMIT 1");
      $stmt-&gt;bind_param("i", $id);    
      $stmt-&gt;execute();
      $stmt-&gt;store_result();
      $num_returns = $stmt-&gt;num_rows;
      $stmt-&gt;close();
  
      if ($num_returns &gt; 0)
      {
          return true;
      }
      else
      {
          return false;   
      }
  }
  
  function permissionNameExists($permission)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("SELECT id
          FROM ".$db_table_prefix."permissions
          WHERE
          name = ?
          LIMIT 1");
      $stmt-&gt;bind_param("s", $permission);    
      $stmt-&gt;execute();
      $stmt-&gt;store_result();
      $num_returns = $stmt-&gt;num_rows;
      $stmt-&gt;close();
  
      if ($num_returns &gt; 0)
      {
          return true;
      }
      else
      {
          return false;   
      }
  }
  
  function updatePermissionName($id, $name)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("UPDATE ".$db_table_prefix."permissions
          SET name = ?
          WHERE
          id = ?
          LIMIT 1");
      $stmt-&gt;bind_param("si", $name, $id);
      $result = $stmt-&gt;execute();
      $stmt-&gt;close(); 
      return $result; 
  }
  
  
  function addPermission($permission, $user) {
      global $mysqli,$db_table_prefix; 
      $i = 0;
      $stmt = $mysqli-&gt;prepare("INSERT INTO ".$db_table_prefix."user_permission_matches (
          permission_id,
          user_id
          )
          VALUES (
          ?,
          ?
          )");
      if (is_array($permission)){
          foreach($permission as $id){
              $stmt-&gt;bind_param("ii", $id, $user);
              $stmt-&gt;execute();
              $i++;
          }
      }
      elseif (is_array($user)){
          foreach($user as $id){
              $stmt-&gt;bind_param("ii", $permission, $id);
              $stmt-&gt;execute();
              $i++;
          }
      }
      else {
          $stmt-&gt;bind_param("ii", $permission, $user);
          $stmt-&gt;execute();
          $i++;
      }
      $stmt-&gt;close();
      return $i;
  }
  
  function fetchAllMatches()
  {
      global $mysqli,$db_table_prefix; 
      $stmt = $mysqli-&gt;prepare("SELECT 
          id,
          user_id,
          permission_id
          FROM ".$db_table_prefix."user_permission_matches");
      $stmt-&gt;execute();
      $stmt-&gt;bind_result($id, $user, $permission);
      while ($stmt-&gt;fetch()){
          $row[] = array('id' =&gt; $id, 'user_id' =&gt; $user, 'permission_id' =&gt; $permission);
      }
      $stmt-&gt;close();
      return ($row);  
  }
  
  function fetchUserPermissions($user_id)
  {
      global $mysqli,$db_table_prefix; 
      $stmt = $mysqli-&gt;prepare("SELECT
          id,
          permission_id
          FROM ".$db_table_prefix."user_permission_matches
          WHERE user_id = ?
          ");
      $stmt-&gt;bind_param("i", $user_id);   
      $stmt-&gt;execute();
      $stmt-&gt;bind_result($id, $permission);
      while ($stmt-&gt;fetch()){
          $row[$permission] = array('id' =&gt; $id, 'permission_id' =&gt; $permission);
      }
      $stmt-&gt;close();
      if (isset($row)){
          return ($row);
      }
  }
  
  function fetchPermissionUsers($permission_id)
  {
      global $mysqli,$db_table_prefix; 
      $stmt = $mysqli-&gt;prepare("SELECT id, user_id
          FROM ".$db_table_prefix."user_permission_matches
          WHERE permission_id = ?
          ");
      $stmt-&gt;bind_param("i", $permission_id); 
      $stmt-&gt;execute();
      $stmt-&gt;bind_result($id, $user);
      while ($stmt-&gt;fetch()){
          $row[$user] = array('id' =&gt; $id, 'user_id' =&gt; $user);
      }
      $stmt-&gt;close();
      if (isset($row)){
          return ($row);
      }
  }
  
  function removePermission($permission, $user) {
      global $mysqli,$db_table_prefix; 
      $i = 0;
      $stmt = $mysqli-&gt;prepare("DELETE FROM ".$db_table_prefix."user_permission_matches 
          WHERE permission_id = ?
          AND user_id =?");
      if (is_array($permission)){
          foreach($permission as $id){
              $stmt-&gt;bind_param("ii", $id, $user);
              $stmt-&gt;execute();
              $i++;
          }
      }
      elseif (is_array($user)){
          foreach($user as $id){
              $stmt-&gt;bind_param("ii", $permission, $id);
              $stmt-&gt;execute();
              $i++;
          }
      }
      else {
          $stmt-&gt;bind_param("ii", $permission, $user);
          $stmt-&gt;execute();
          $i++;
      }
      $stmt-&gt;close();
      return $i;
  }
  
  //Update configuration table
  function updateConfig($id, $value)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("UPDATE ".$db_table_prefix."configuration
          SET 
          value = ?
          WHERE
          id = ?");
      foreach ($id as $cfg){
          $stmt-&gt;bind_param("si", $value[$cfg], $cfg);
          $stmt-&gt;execute();
      }
      $stmt-&gt;close(); 
  }
  
  function createPages($pages) {
      global $mysqli,$db_table_prefix; 
      $stmt = $mysqli-&gt;prepare("INSERT INTO ".$db_table_prefix."pages (
          page
          )
          VALUES (
          ?
          )");
      foreach($pages as $page){
          $stmt-&gt;bind_param("s", $page);
          $stmt-&gt;execute();
      }
      $stmt-&gt;close();
  }
  
  
  function deletePages($pages) {
      global $mysqli,$db_table_prefix; 
      $stmt = $mysqli-&gt;prepare("DELETE FROM ".$db_table_prefix."pages 
          WHERE id = ?");
      $stmt2 = $mysqli-&gt;prepare("DELETE FROM ".$db_table_prefix."permission_page_matches 
          WHERE page_id = ?");
      foreach($pages as $id){
          $stmt-&gt;bind_param("i", $id);
          $stmt-&gt;execute();
          $stmt2-&gt;bind_param("i", $id);
          $stmt2-&gt;execute();
      }
      $stmt-&gt;close();
      $stmt2-&gt;close();
  }
  
  
  function fetchAllPages()
  {
      global $mysqli,$db_table_prefix; 
      $stmt = $mysqli-&gt;prepare("SELECT 
          id,
          page,
          private
          FROM ".$db_table_prefix."pages");
      $stmt-&gt;execute();
      $stmt-&gt;bind_result($id, $page, $private);
      while ($stmt-&gt;fetch()){
          $row[$page] = array('id' =&gt; $id, 'page' =&gt; $page, 'private' =&gt; $private);
      }
      $stmt-&gt;close();
      if (isset($row)){
          return ($row);
      }
  }
  
  
  function fetchPageDetails($id)
  {
      global $mysqli,$db_table_prefix; 
      $stmt = $mysqli-&gt;prepare("SELECT 
          id,
          page,
          private
          FROM ".$db_table_prefix."pages
          WHERE
          id = ?
          LIMIT 1");
      $stmt-&gt;bind_param("i", $id);
      $stmt-&gt;execute();
      $stmt-&gt;bind_result($id, $page, $private);
      while ($stmt-&gt;fetch()){
          $row = array('id' =&gt; $id, 'page' =&gt; $page, 'private' =&gt; $private);
      }
      $stmt-&gt;close();
      return ($row);
  }
  
  
  function pageIdExists($id)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("SELECT private
          FROM ".$db_table_prefix."pages
          WHERE
          id = ?
          LIMIT 1");
      $stmt-&gt;bind_param("i", $id);    
      $stmt-&gt;execute();
      $stmt-&gt;store_result();  
      $num_returns = $stmt-&gt;num_rows;
      $stmt-&gt;close();
  
      if ($num_returns &gt; 0)
      {
          return true;
      }
      else
      {
          return false;   
      }
  }
  
  
  function updatePrivate($id, $private)
  {
      global $mysqli,$db_table_prefix;
      $stmt = $mysqli-&gt;prepare("UPDATE ".$db_table_prefix."pages
          SET 
          private = ?
          WHERE
          id = ?");
      $stmt-&gt;bind_param("ii", $private, $id);
      $result = $stmt-&gt;execute();
      $stmt-&gt;close(); 
      return $result; 
  }
  
  function addPage($page, $permission) {
      global $mysqli,$db_table_prefix; 
      $i = 0;
      $stmt = $mysqli-&gt;prepare("INSERT INTO ".$db_table_prefix."permission_page_matches (
          permission_id,
          page_id
          )
          VALUES (
          ?,
          ?
          )");
      if (is_array($permission)){
          foreach($permission as $id){
              $stmt-&gt;bind_param("ii", $id, $page);
              $stmt-&gt;execute();
              $i++;
          }
      }
      elseif (is_array($page)){
          foreach($page as $id){
              $stmt-&gt;bind_param("ii", $permission, $id);
              $stmt-&gt;execute();
              $i++;
          }
      }
      else {
          $stmt-&gt;bind_param("ii", $permission, $page);
          $stmt-&gt;execute();
          $i++;
      }
      $stmt-&gt;close();
      return $i;
  }
  
  function fetchPagePermissions($page_id)
  {
      global $mysqli,$db_table_prefix; 
      $stmt = $mysqli-&gt;prepare("SELECT
          id,
          permission_id
          FROM ".$db_table_prefix."permission_page_matches
          WHERE page_id = ?
          ");
      $stmt-&gt;bind_param("i", $page_id);   
      $stmt-&gt;execute();
      $stmt-&gt;bind_result($id, $permission);
      while ($stmt-&gt;fetch()){
          $row[$permission] = array('id' =&gt; $id, 'permission_id' =&gt; $permission);
      }
      $stmt-&gt;close();
      if (isset($row)){
          return ($row);
      }
  }
  
  function fetchPermissionPages($permission_id)
  {
      global $mysqli,$db_table_prefix; 
      $stmt = $mysqli-&gt;prepare("SELECT
          id,
          page_id
          FROM ".$db_table_prefix."permission_page_matches
          WHERE permission_id = ?
          ");
      $stmt-&gt;bind_param("i", $permission_id); 
      $stmt-&gt;execute();
      $stmt-&gt;bind_result($id, $page);
      while ($stmt-&gt;fetch()){
          $row[$page] = array('id' =&gt; $id, 'permission_id' =&gt; $page);
      }
      $stmt-&gt;close();
      if (isset($row)){
          return ($row);
      }
  }
  
  function removePage($page, $permission) {
      global $mysqli,$db_table_prefix; 
      $i = 0;
      $stmt = $mysqli-&gt;prepare("DELETE FROM ".$db_table_prefix."permission_page_matches 
          WHERE page_id = ?
          AND permission_id =?");
      if (is_array($page)){
          foreach($page as $id){
              $stmt-&gt;bind_param("ii", $id, $permission);
              $stmt-&gt;execute();
              $i++;
          }
      }
      elseif (is_array($permission)){
          foreach($permission as $id){
              $stmt-&gt;bind_param("ii", $page, $id);
              $stmt-&gt;execute();
              $i++;
          }
      }
      else {
          $stmt-&gt;bind_param("ii", $permission, $user);
          $stmt-&gt;execute();
          $i++;
      }
      $stmt-&gt;close();
      return $i;
  }
  
  function securePage($uri){
  
  
      $tokens = explode('/', $uri);
      $page = $tokens[sizeof($tokens)-1];
      global $mysqli,$db_table_prefix,$loggedInUser;
  
      $stmt = $mysqli-&gt;prepare("SELECT 
          id,
          page,
          private
          FROM ".$db_table_prefix."pages
          WHERE
          page = ?
          LIMIT 1");
      $stmt-&gt;bind_param("s", $page);
      $stmt-&gt;execute();
      $stmt-&gt;bind_result($id, $page, $private);
      while ($stmt-&gt;fetch()){
          $pageDetails = array('id' =&gt; $id, 'page' =&gt; $page, 'private' =&gt; $private);
      }
      $stmt-&gt;close();
  
      if (empty($pageDetails)){
          return true;
      }
  
      elseif ($pageDetails['private'] == 0) {
          return true;    
      }
  
      elseif(!isUserLoggedIn()) 
      {
          header("Location: login.php");
          return false;
      }
      else {
  
          $stmt = $mysqli-&gt;prepare("SELECT
              permission_id
              FROM ".$db_table_prefix."permission_page_matches
              WHERE page_id = ?
              ");
          $stmt-&gt;bind_param("i", $pageDetails['id']); 
          $stmt-&gt;execute();
          $stmt-&gt;bind_result($permission);
          while ($stmt-&gt;fetch()){
              $pagePermissions[] = $permission;
          }
          $stmt-&gt;close();
  
          if ($loggedInUser-&gt;checkPermission($pagePermissions)){ 
              return true;
          }
  
          elseif ($loggedInUser-&gt;user_id == $master_account){
              return true;
          }
          else {
              header("Location: account.php");
              return false;   
          }
      }
  }
  
  ?&gt;
  </code></pre>