Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6713107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:20:24+00:00 2026-05-26T08:20:24+00:00

I forget where I copied it from (I think a user posted it on

  • 0

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:

    function fetch($result){   
    $array = array();           
    if($result instanceof mysqli_stmt){
        $result->store_result();
        $variables = array();
        $data = array();
        $meta = $result->result_metadata();                 
        while($field = $meta->fetch_field()){                   
            $variables[] = &$data[$field->name];
        }               
        call_user_func_array(array($result, 'bind_result'), $variables);
        $i=0;                               
        while($result->fetch()){                                    
            $array[$i] = array();
            foreach($data as $k=>$v)
            $array[$i][$k] = $v;
            $i++;                   
        }               
    }elseif($result instanceof mysqli_result){
        while($row = $result->fetch_assoc())
        $array[] = $row;
    }           
    return $array;
}

I’ve been trying to use it like so (all these functions are methods of the class $database):

    function query($str, $values){                              
    if($stmt=$this->sql->prepare($str)){                    
        $types=$this->castArrayAsString($values); //returns a string of $values's types for bind_params             
        array_unshift($values, $types);         
        call_user_func_array(array(&$stmt, 'bind_param'), $this->makeValuesReferenced($values));                        
        $stmt->execute();
        $result=$this->fetch($stmt); //the function I'm having problems with                                                                            
        if(empty($result)){                                     
            $return=true;                   
        }else{
            $return=$result;
        }       
        $stmt->close();         
    }else{
        echo $this->sql->error;
        $return=false;  
    }                       
    return $return;             
}

This returns nothing:

$list=$database->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));

This works fine:

$list=$database->query("SELECT field1, field2 FROM table1 WHERE someid=?", array($someid));

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.

Any help is appreciated,

Thanks

EDIT HERE IS THE WHOLE CLASS

<?php

class database{

    var $HOST="xx.xx.xx.xxx";   
    var $USER="xxxxxxxxxxx";    
    var $PASS='xxxxxxxxxx';

    var $DATABASE="my_database";

    var $sql;

    function database(){            
        $this->sql=new mysqli($this->HOST, $this->USER, $this->PASS, $this->DATABASE);              
        if($this->sql->connect_errno){
            echo "ERROR - No MySQL connection: ".$mysqli->connect_error;
            exit;
        }                   
    }

    function query($str, $values){                                          
        if($stmt=$this->sql->prepare($str)){                            
            $types=$this->castArrayAsString($values);               
            array_unshift($values, $types);         
            call_user_func_array(array(&$stmt, 'bind_param'), $this->makeValuesReferenced($values));                                
            $stmt->execute();                               
            $result=$this->fetch($stmt);                                                                                        
            if(empty($result)){                                     
                $return=true;                   
            }else{
                $return=$result;
            }                       
            $stmt->close();         
        }else{
            echo $this->sql->error;
            $return=false;  
        }                       
        return $return;             
    }

    function fetch($result){   
        $array = array();           
        if($result instanceof mysqli_stmt){
            $result->store_result();
            $variables = array();
            $data = array();
            $meta = $result->result_metadata();                 
            while($field = $meta->fetch_field()){                   
                $variables[] = &$data[$field->name];
            }               
            call_user_func_array(array($result, 'bind_result'), $variables);
            $i=0;                               
            while($result->fetch()){                                    
                $array[$i] = array();
                foreach($data as $k=>$v)
                $array[$i][$k] = $v;
                $i++;                   
            }               
        }elseif($result instanceof mysqli_result){
            while($row = $result->fetch_assoc())
            $array[] = $row;
        }           
        return $array;
    }

    function close(){
        $this->sql->close();    
    }

    function castArrayAsString($array){         
        $types="";
        foreach($array as $key => $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 => $value){
            $refs[$key] = &$arr[$key];
        }
        return $refs;       
    }

}

$database=new database;

?>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:20:25+00:00Added an answer on May 26, 2026 at 8:20 am

    The query that returns 0 result set is doing an ‘inner-join’, which means that the source table and the target table must share the same value for the columns being linked against in the query (in this case a.id_table2 and b.id). Chances are you don’t have any matching values so you are getting a 0 result set. Try a left outer join.


    Try the following and see if your results are returned:

        "SELECT a.field1, b.field2 FROM table1 AS a INNER JOIN table2 AS b ON a.id_table2=b.id WHERE a.someid = :someId"
    
        $dbo = new Database();
    
        $stmt = $dbo->sql->prepare($sql);
    
        $stmt->bindValue(':someId', $someId, PDO::PARAM_INT);
    
        $stmt->execute();
    
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            var_dump($row);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, this is rather simple, but from what I've seen… you can only use
I'm learning C right now, and I copied this little snippet straight from the
I always forget how to do things like this. I have a database table
If you read this thread before - forget everything I wrote , I must
I've been using File.ReadAllText() to open a CSV file, but every time I forget
How can I permanently forget T4 generated files from being version control using Mercurial?
When i try to launch the android emulator from the avd manager this error
Background I've written a small C#/.NET 4.0 application that syncs various settings from a
Following is a basic implementation of the Xorshift RNG (copied from the Wikipedia): uint32_t
I often forget about the regular expression modifiers m and s and their differences.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.