title: PHP MySQLi Prepared Statement: Fetch Not Returning Results for Joins
body: <p>I forget where I copied it from (I think a user posted it on php.net) but this function for fetching results of prepared and normal MySQLi statements is not returning any results when used with statements that contain a simple INNER JOIN, at least the one I tried:</p>
  
  <pre><code>    function fetch($result){   
      $array = array();           
      if($result instanceof mysqli_stmt){
          $result-&gt;store_result();
          $variables = array();
          $data = array();
          $meta = $result-&gt;result_metadata();                 
          while($field = $meta-&gt;fetch_field()){                   
              $variables[] = &amp;$data[$field-&gt;name];
          }               
          call_user_func_array(array($result, 'bind_result'), $variables);
          $i=0;                               
          while($result-&gt;fetch()){                                    
              $array[$i] = array();
              foreach($data as $k=&gt;$v)
              $array[$i][$k] = $v;
              $i++;                   
          }               
      }elseif($result instanceof mysqli_result){
          while($row = $result-&gt;fetch_assoc())
          $array[] = $row;
      }           
      return $array;
  }
  </code></pre>
  
  <p>I've been trying to use it like so (all these functions are methods of the class $database):</p>
  
  <pre><code>    function query($str, $values){                              
      if($stmt=$this-&gt;sql-&gt;prepare($str)){                    
          $types=$this-&gt;castArrayAsString($values); //returns a string of $values's types for bind_params             
          array_unshift($values, $types);         
          call_user_func_array(array(&amp;$stmt, 'bind_param'), $this-&gt;makeValuesReferenced($values));                        
          $stmt-&gt;execute();
          $result=$this-&gt;fetch($stmt); //the function I'm having problems with                                                                            
          if(empty($result)){                                     
              $return=true;                   
          }else{
              $return=$result;
          }       
          $stmt-&gt;close();         
      }else{
          echo $this-&gt;sql-&gt;error;
          $return=false;  
      }                       
      return $return;             
  }
  </code></pre>
  
  <p>This returns nothing:</p>
  
  <pre><code>$list=$database-&gt;query("SELECT a.field1, b.field2 FROM table1 AS a INNER JOIN table2 AS b ON a.id_table2=b.id WHERE a.someid=?", array($someid));
  </code></pre>
  
  <p>This works fine:</p>
  
  <pre><code>$list=$database-&gt;query("SELECT field1, field2 FROM table1 WHERE someid=?", array($someid));
  </code></pre>
  
  <p>If I print out the meta data from fetching the result, it shows that fields are selected, etc, but still gives no final results in the fetch() func.</p>
  
  <p>Any help is appreciated,</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT HERE IS THE WHOLE CLASS</strong></p>
  
  <pre><code>&lt;?php
  
  class database{
  
      var $HOST="xx.xx.xx.xxx";   
      var $USER="xxxxxxxxxxx";    
      var $PASS='xxxxxxxxxx';
  
      var $DATABASE="my_database";
  
      var $sql;
  
      function database(){            
          $this-&gt;sql=new mysqli($this-&gt;HOST, $this-&gt;USER, $this-&gt;PASS, $this-&gt;DATABASE);              
          if($this-&gt;sql-&gt;connect_errno){
              echo "ERROR - No MySQL connection: ".$mysqli-&gt;connect_error;
              exit;
          }                   
      }
  
      function query($str, $values){                                          
          if($stmt=$this-&gt;sql-&gt;prepare($str)){                            
              $types=$this-&gt;castArrayAsString($values);               
              array_unshift($values, $types);         
              call_user_func_array(array(&amp;$stmt, 'bind_param'), $this-&gt;makeValuesReferenced($values));                                
              $stmt-&gt;execute();                               
              $result=$this-&gt;fetch($stmt);                                                                                        
              if(empty($result)){                                     
                  $return=true;                   
              }else{
                  $return=$result;
              }                       
              $stmt-&gt;close();         
          }else{
              echo $this-&gt;sql-&gt;error;
              $return=false;  
          }                       
          return $return;             
      }
  
      function fetch($result){   
          $array = array();           
          if($result instanceof mysqli_stmt){
              $result-&gt;store_result();
              $variables = array();
              $data = array();
              $meta = $result-&gt;result_metadata();                 
              while($field = $meta-&gt;fetch_field()){                   
                  $variables[] = &amp;$data[$field-&gt;name];
              }               
              call_user_func_array(array($result, 'bind_result'), $variables);
              $i=0;                               
              while($result-&gt;fetch()){                                    
                  $array[$i] = array();
                  foreach($data as $k=&gt;$v)
                  $array[$i][$k] = $v;
                  $i++;                   
              }               
          }elseif($result instanceof mysqli_result){
              while($row = $result-&gt;fetch_assoc())
              $array[] = $row;
          }           
          return $array;
      }
  
      function close(){
          $this-&gt;sql-&gt;close();    
      }
  
      function castArrayAsString($array){         
          $types="";
          foreach($array as $key =&gt; $value) {              
              if(is_numeric($value)){
                  if(is_float($value)){
                      $types.="d";
                  }else{
                      $types.="i";
                  }                   
              }else{
                  $types.="s";
              }                        
          }           
          return $types;  
      }
  
      function makeValuesReferenced($arr){
          $refs=array();
          foreach($arr as $key =&gt; $value){
              $refs[$key] = &amp;$arr[$key];
          }
          return $refs;       
      }
  
  }
  
  $database=new database;
  </code></pre>
  
  <p>?></p>