title: How can I use mysqli_query inside PHP class?
body: <p>How can i use <code>$connect</code> variable inside <code>User</code> class with out sending <code>$connect</code> as parameter or defines <code>global $connect</code> inside User class?</p>
  
  <p>And I want to create only one connection is </p>
  
  <pre><code>$connect = mysqli_connect('localhost', 'root', '1234', 'mydatabase');
  </code></pre>
  
  <p><strong>The error</strong></p>
  
  <p><strong>Warning:</strong> mysqli_query() expects parameter 1 to be mysqli, null given in <strong>D:\xampp\htdocs\mateocode\index.php</strong> on <strong>line 9</strong></p>
  
  <p><strong>Example code</strong></p>
  
  <pre><code>&lt;?php
      class User {    
          public $id;
          public $username;
          function __construct($user_id)
          {   
          $sql = "select id, username from user where id = '$user_id' limit 1";
  
          $result = mysqli_query($connect, $sql);    
          if(!$result) return false;
  
          if(mysqli_num_rows($result)&gt;0){
              $data = mysqli_fetch_assoc($result);
              $this-&gt;id = $data['id'];
              $this-&gt;username = $data['username'];
          }else{ 
              return false; 
          }   
      }
  }
  
  $connect = mysqli_connect('localhost', 'root', '1234', 'mydatabase');
  
  if($objUser = new User(1))
      echo $objUser-&gt;username;
  else 
      echo 'There was an error or user not found!'; 
  </code></pre>