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 8642603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:49:04+00:00 2026-06-12T11:49:04+00:00

Having some trouble with the following code. I’ve created a class to manage the

  • 0

Having some trouble with the following code. I’ve created a class to manage the DB connection, using what you see below as queryPreparedQuery and works fine when getting data for a single user, or any data that returns a single result using something like this…

include 'stuff/class_stuff.php';

function SweetStuff() {

    $foo = new db_connection();
    $foo->queryPreparedQuery("SELECT Bacon, Eggs, Coffee FROM Necessary_Items WHERE Available = ?",$bool);
    $bar = $foo->Load();
    $stuff = 'Brand of Pork is '.$bar['Bacon'].' combined with '.$bar['Eggs'].' eggs and '.$bar['Coffee'].' nectar for energy and heart failure.';

    return $stuff;

}

echo SweetStuff();

Problem is, I want to build the functionality in here to allow for a MySQL query which returns multiple results. What am I missing? I know it’s staring me right in the face…

class db_connection
{
    private $conn;
    private $stmt;
    private $result;

    #Build a mysql connection
    public function __construct($host="HOST", $user="USER", $pass="PASS", $db="DB_NAME")
    {
        $this->conn = new mysqli($host, $user, $pass, $db);

        if(mysqli_connect_errno())
        {
            echo("Database connect Error : "
            . mysqli_connect_error());
        }
    }
    #return the connected connection
    public function getConnect()
    {
        return $this->conn;
    }
    #execute a prepared query without selecting
    public function execPreparedQuery($query, $params_r)
    {
        $stmt =  $this->conn->stmt_init();
        if (!$stmt->prepare($query))
        {
            echo("Error in $statement when preparing: "
            . mysqli_error($this->conn));
            return 0;
        }
        $types = '';
        $values = '';
        $index = 0;
        if(!is_array($params_r))
        $params_r = array($params_r);
        $bindParam = '$stmt->bind_param("';
        foreach($params_r as $param)
        {

            if (is_numeric($param)) {
                $types.="i";
            }
            elseif (is_float($param)) {
                $types.="d";
            }else{
                $types.="s";
            }
            $values .=  '$params_r[' . $index . '],';
            $index++;
        }
        $values = rtrim($values, ',');
        $bindParam .= $types . '", ' . $values . ');';      

        if (strlen($types) > 0)
        {
            //for debug
            //if(strpos($query, "INSERT") > 0)
            //var_dump($params_r);
            eval($bindParam);
        }

        $stmt->execute();       
        return $stmt;
    }
    #execute a prepared query
    public function queryPreparedQuery($query, $params_r)
    {
        $this->stmt = $this->execPreparedQuery($query, $params_r);
        $this->stmt->store_result();
        $meta = $this->stmt->result_metadata();
        $bindResult = '$this->stmt->bind_result(';
        while ($columnName = $meta->fetch_field()) {
            $bindResult .= '$this->result["'.$columnName->name.'"],';
        }
        $bindResult = rtrim($bindResult, ',') . ');';
        eval($bindResult);
    }
    #Load result
    public function Load(&$result = null)
    {       
        if (func_num_args() == 0)
        {
            $this->stmt->fetch();
            return $this->result;
        }
        else
        {
            $res = $this->stmt->fetch();
            $result = $this->result;
            return $res;
        }
    }

    #Load result
    public function Execute(&$result = null)
    {       
        if (func_num_args() == 0)
        {
            $this->stmt->fetch_array();
            return $this->result;
        }
        else
        {
            $res = $this->stmt->fetch_array();
            $result = $this->result;
            return $res;
        }
    }   

    private function bindParameters(&$obj, &$bind_params_r)
    {
        call_user_func_array(array($obj, "bind_param"), $bind_params_r);
    }

}

UPDATE

Got this to work with Patrick’s help. Was able to find the following code with the help of this question, and with a few tweaks, it works beautifully. Added the following after the execute() statement in ExecPreparedQuery, returning an array at the very end instead of the single result:

    # these lines of code below return multi-dimentional/ nested array, similar to mysqli::fetch_all()
    $stmt->store_result();

    $variables = array();
    $data = array();
    $meta = $stmt->result_metadata();

    while($field = $meta->fetch_field())
        $variables[] = &$data[$field->name]; // pass by reference

    call_user_func_array(array($stmt, 'bind_result'), $variables);

    $i=0;
    while($stmt->fetch())
    {
        $array[$i] = array();
        foreach($data as $k=>$v)
            $array[$i][$k] = $v;
        $i++;
    }

    # close statement
    $stmt->close();

    return $array;

As a result of the altered code, I changed the call to interpret multidimensional array data rather than a single result, of course. Thanks again!

  • 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-06-12T11:49:05+00:00Added an answer on June 12, 2026 at 11:49 am

    In your Execute function you are calling $this->stmt>fetch_array().

    That function only returns an array of a single row of the result set.

    You probably want:

    $this->stmt->fetch_all()

    Update

    To retrieve the entire result set from a prepared statement:

    $this->stmt->store_result()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some trouble deleting documents from Solr index. I use following code:
I am having some trouble updating UMDF drivers using devcon during a standard code-deploy-debug
I'm having some trouble with the following code, more than likely a n00b error
I'm having some trouble converting the following code from c++ to c# because of
I'm having some trouble understanding the following block of code: void InsertSorted(Entry * &
I'm having some trouble getting a view to flip. I have the following code
im having some trouble with the following code: Ext.define('...controller...', { extend: 'Ext.app.Controller', init: function()
I was having some trouble with the following code. $(this) appeared to be undefined.
I'm having some trouble in the following code. public ArrayList<? extends IEvent> getEventsByDateRange(DateTime minStartTime,
I am having some trouble understanding the following simple C code: int main(int argc,

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.