Getting and posting data use PHP OOP and MySQLi I am not very experienced in PHP OOP. Moreover, when MySQLi comes with PHP OOP, it makes me more confused about using it in the most efficient way.

Connection class:

&lt;?php
//connectionclass.php
class connection{
public $conn;
public $warn;
public $err;

function __construct(){
  $this-&gt;connect();
}

private function connect(){
  $this-&gt;conn = @ new mysqli('localhost', 'sever_user', 'user_password');
    if ($this-&gt;conn-&gt;connect_error) {
      $this-&gt;conn = FALSE;
      $this-&gt;warn = '&lt;br /&gt;Failed to connect database! Please try again later';
     }
}

public function get_data($qry){
  $result = $this-&gt;conn-&gt;query($qry);
  if($result-&gt;num_rows&gt;=1){
    return $result;
  }else{
    $this-&gt;err = $this-&gt;conn-&gt;error;
    return FALSE;
  }
  $result-&gt;free();
}

public function post_data($qry){
  $this-&gt;conn-&gt;query($qry);
  if($this-&gt;conn-&gt;affected_rows&gt;=1){
    return TRUE;
  }else{
    $this-&gt;err = $this-&gt;conn-&gt;error;
    return FALSE;
  }
}
}
?&gt;


Structure of a PHP page which uses a MySQL database to store and get data:

&lt;?php
//login.php
include('/include/connectionclass.php');
$db = new connection();

$query = "SELECT * FROM USERS WHERE user_country='India'";
$data = $db-&gt;get_data($query);
  if($data){
    while($row=$data-&gt;fetch_assoc()){
      echo 'User Name: ':.$row["user_name"].' Age: '.$row["age"];
    }
  }
?&gt;


My login.php uses a connection class to get data about users. All the things are running well, but one thing confused me. In connectionclass.php, $this-&gt;conn is itself an object as it calls new mysqli, so this is an object inside another object $db. Moreover, When I am using $data = $db-&gt;get_data($query);, a result set is created inside a $db object by method get_data, then this result set is copied into a variable $data inside the login.php page.

Actually, two instances of the same result sets/data sets are creating here, one inside the db object and one inside the login page.

Is this the right way to use MySQLi and PHP to get a dataset from a MySQL database? Will it use more memory and server resources when the dataset is larger (when have to get large amount of data for many users)?

If it is not right way, please explain your points and provide another solution which can be used efficiently for PHP OOP and MySQLi.
No, this isn't the right way to use OOP or MySQLi.

First of all, you only provide two methods to access your wrapped mysqli instance: one to get data, one to post data. 

This hides a lot of useful and necessary functions. You can't even use prepared statements with your code, making it incredibly insecure. 

You also didn't really gain anything with your functions. With your functions, your code is:


$query = "SELECT * FROM USERS WHERE user_country='India'";
$data = $db-&gt;get_data($query);
  if($data){
    while($row=$data-&gt;fetch_assoc()){
      echo 'User Name: ':.$row["user_name"].' Age: '.$row["age"];
    }
  }



Without your functions it would be:

$query = "SELECT * FROM USERS WHERE user_country='India'";
$data = $db-&gt;query($query);
  if($data){
    while($row=$data-&gt;fetch_assoc()){
      echo 'User Name: ':.$row["user_name"].' Age: '.$row["age"];
    }
$data-&gt;free();


It's essentially the same code, making your functions unnecessary. 

If you do want to wrap mysqli - which may not actually be necessary in your case -, make sure that you:


provide all the needed functionalities.
actually improve mysqli (make it easier to use, add functionalities, ...).
keep the interface as close to that of mysqli as possible, to make it easier to use.


Instead of wrapping mysqli, it may make sense to aim for less abstract methods. For example, you may have a UserDAO, which may have methods such as selectByCountry.