title: How to return mysqli result object from a function in a class?
body: <p>So.. I am practicing my PHP by writing a database object with CRUD operations.. However, I am having trouble returning a result object from the database object.</p>
  
  <p>This is the relevant "select_where" function which lives inside the db Class and takes one parameter specifying the table name:</p>
  
  <pre><code>       function select_where(array $values, $table, $what = "*")
      {
          $where_stmt = '';
  
          if(count($values)&gt;0)
          {
              $fields = array_keys($values);
              $vals   = array_values($values);
  
              if ($what != "*")
              {
                  if(is_array($what)===TRUE)
                  {
                      $what = implode($what, ',');
                  }
                  else //assume string! -- needs better error handling!
                  {
                  //Don;t do  anything for now
                  }
  
              }
  
              $where_stmt = "WHERE ";
  
              for( $i=0 ; $i&lt;count($fields) ; $i++)
              {
                  $where_stmt .= "$field[$i] = '$vals[$i]' AND ";
  
              }
  
              $where_stmt .= "$field[$i] = '$vals[$i]'";
  
  
          }//else: array is an empty one, meaning SELECT ALL;
  
  
          $query = "SELECT $what FROM `$table` $where_stmt;";
          ;
  
          if($result = $this-&gt;mysqli_conn-&gt;query($query))
          {
              echo "RESULT:&lt;br /&gt;";
              print_r($result);
              return $result;
  
          }else{
              return false;
          }
  
      }
  </code></pre>
  
  <p>The print_r function prints the attributes of the result object with no issues.. BUT when I call my function, it does not.. As so:</p>
  
  <pre><code>    $db = new db();
  
      $result = $db-&gt;select_all('codes');
  
      print_r($result);
  </code></pre>
  
  <p>print_r does not print anything...</p>
  
  <p>I am 100% sure that this has to do with my lack of understanding of how PHP OOP works; while I look for answers elsewhere, any suggestions or solutions are highly appreciated!</p>
  
  <hr>
  
  <p>LOOKS LIKE I've Found the answer:</p>
  
  <p>ROOKIE MISTAKE!</p>
  
  <p>In my code, I used the helper function
       $result = $db->select_all('codes');</p>
  
  <p>which actually looked like this:</p>
  
  <pre><code>function select_all($table, $what="*" )
      {
          $empty_array = array();
          $this-&gt;select_where($empty_array, $table, $what);
  
      }
  </code></pre>
  
  <p>But as one can see, there was no return from THIS function! So now:</p>
  
  <pre><code>function select_all($table, $what="*" )
      {
          $empty_array = array();
          return $this-&gt;select_where($empty_array, $table, $what);
  
      }
  </code></pre>
  
  <p>is the solution! </p>
  
  <p>Thank you again</p>