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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:30:38+00:00 2026-06-18T16:30:38+00:00

I am trying to write a very small abstraction layer for my mysqli connection

  • 0

I am trying to write a very small abstraction layer for my mysqli connection and have run into a problem.
Since I am maintaining older code I need to get an associative array from my query as this is the way the code has been set up and therefor less work for me once this works…
This function does work with all kinds of queries(not just select)…

my function I wrote is this:

function connectDB($query,$v=array()) {
    $mysqli = new mysqli(HOST,USER,PW,DATABASE);

    if($res=$mysqli->prepare($query)) {
            //dynamically bind all $v
            if($v) {
            $values=array($v[0]);
            for($i=1; $i<count($v); $i++) {
                ${'bind'.$i}=$v[$i];
                $values[]=&${'bind'.$i};
            }
            call_user_func_array(array($res,'bind_param'),$values);
        }
        $res->execute();

        //bind all table rows to result
        if(strtolower(substr($query,0,6))=="select") {
            $fields=array();
            $meta=$res->result_metadata();
            while($field=$meta->fetch_field()) { 
                ${$field->name}=null;
                $fields[$field->name]=&${$field->name};
            }
            call_user_func_array(array($res,"bind_result"),$fields);

            //return associative array
            $results = array();
            $i=0;
            while($res->fetch()) {
                $results[$i]=array();
                foreach($fields as $k => $v) $results[$i][$k] = $v;
                $i++;
            }
        }
        else {
            $results=$mysqli->affected_rows;
            if($mysqli->affected_rows<1) $results=$mysqli->info;
        }

        $res->close();
    }
    $mysqli->close();

    return $results;
}

so if I call:

$MySqlres=connectDB("select * from `modx_events` events  limit 1");
var_dump($MySqlres);

I get a nice associative array with the content of my select.

Now unfortunately the following mysql query will return NULL as a value to all of it’s array keys:

$MySqlres=connectDB("select *, events.`id` as `ID`,venues.`name` as `venueName`,
venues.`suburb` as `venueSuburb`,venues.`advertiser` as `venueAdvertiser`
from `modx_events` events left join `modx_venues` venues on events.`venue`=venues.`id`
where events.`id`!='e' order by events.`start_date` asc, venues.`name` limit 1");

(the same query runs as pure SQL and will return the correct values)
Any idea what this could be? Does my associative array function fail? Is there something wrong with the way I implemented the query?

ps: PDO is not an option and mysqlnd is not installed… 🙁

ADDED QUESTION

  • is this too much of overhead just to preserve the associative array return? Should I go with $res->fetch_object() instead?
  • 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-18T16:30:39+00:00Added an answer on June 18, 2026 at 4:30 pm

    I just fixed the function.
    Perhaps this is interesting for someone else:

    function connectDB($mysqli,$query,$v=array()) {
    
        if($mysqli->connect_errno) {
            return array('error'=>'Connect failed: '.$mysqli->connect_error); //error handling here
            exit();
        }
    
        if(substr_count($query,"?")!=strlen($v[0])) {
            return array('error'=>'placeholders and replacements are not equal! placeholders:'.substr_count($query,"?").' replacements:'.strlen($v[0]).' ('.$v[0].')'); //error handling here...
            exit();
        }
    
        if($res=$mysqli->prepare($query)) {
                //dynamically bind all $v
                if($v) {
                $values=array($v[0]);
                for($i=1; $i<count($v); $i++) {
                    ${'bind'.$i}=$v[$i];
                    $values[]=&${'bind'.$i};
                }
                call_user_func_array(array($res,'bind_param'),$values);
            }
            $res->execute();
    
            //bind all table rows to result
            if(strtolower(substr($query,0,6))=="select") {
                $field=$fields=$tempRow=array();
                $meta=$res->result_metadata();
                while($field=$meta->fetch_field()) {
                    $fieldName=$field->name;
                    $fields[]=&$tempRow[$fieldName];
                }
                $meta->free_result();
                call_user_func_array(array($res,"bind_result"),$fields);
    
                //return associative array
                $results=array();
                $i=0;
                while($res->fetch()) {
                    $results[$i]=array();
                    foreach($tempRow as $k=>$v) $results[$i][$k] = $v;
                    $i++;
                }
                $res->free_result();
    
            }
            else { //return infos about the query
                $results["affectedRows"]=$mysqli->affected_rows;
                $results["info"]=$mysqli->info;
                $results["insertID"]=$mysqli->insert_id;
            }
    
            $res->close();
        }
    
        return $results;
    }
    

    cheers

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

Sidebar

Related Questions

I have a small problem and since I am very new to all this
I am trying to write a very thin hypervisor that would have the following
I am trying to write a very small SNMP trap receiver, listening to UDP
I am very new to NAudio and am trying to write a small program
I'm trying to implement a very small shell of my own. I have to
Started by trying to write a small program to translate basic arithmetic into English,
I have a small problem when trying build an XML document with Nokogiri. I
I'm trying to write a small chunk of code to grab the backbuffer into
As a small exercise I am trying to write a very small, simple game
I am trying to write a very simple web-based email client from scratch with

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.