0

Here are two functions I've used for years but after upgrading to the newer version of LAMP I've been getting an error "Uncaught mysqli_sql_exception: Commands out of sync; you can't run this command now" points to the line in the db_fetch function where $stmt->store_result();

function db_query($q) {

    $num_args = func_num_args();
    $arg_list = func_get_args();
    $r = null;

    global $db_obj;

    if($num_args > 1) {
        $stmt = $db_obj->stmt_init();
        if($stmt->prepare($q)) {
            $types = "";
            $refArr = array("");
            for($i = 1; $i < $num_args; $i++) {
                $type = gettype($arg_list[$i]) === "integer" ? "i" : "s";
                $refArr[] = &$arg_list[$i];
                $types .= $type;
            }
            $refArr[0] = $types;
            $ref = new ReflectionClass('mysqli_stmt');
            $method = $ref->getMethod("bind_param");
            $method->invokeArgs($stmt, $refArr);
            $stmt->execute();
            $stmt->store_result();
            $r = $stmt;
        } else {
            db_maybe_handle_error(null, $q);
        }
    } else {
        $r = @$db_obj->query($q);
    }
    db_maybe_handle_error(null, $q);
    return $r;
}

function db_fetch($res, $use_numerical_indices = null) {
    global $db_obj;

    $use_numerical_indices = $use_numerical_indices === true ||
        ($use_numerical_indices === null && DB_DEFAULT_USE_NUMERICAL_INDEXES);

    if(is_array($res)) {
        return array_shift($res);
    }

    if($res instanceof mysqli_stmt) {
        $res->store_result();
        $variables = array();
        $data = array();
        $meta = $res->result_metadata();

        while($field = $meta->fetch_field()) {
            $variables[] = &$data[$field->name];
        }

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

        if(!$res->fetch()) {
            $res->close();
            return null;
        }
        $array = array();
        $i = 0;
        foreach($data as $k => $v) {
            $array[$k] = $v;
            if($use_numerical_indices) {
                $array[$i] = $v;
            }
            $i++;
        }
        db_maybe_handle_error($array);
    }
    $result_type = $use_numerical_indices ? $db_obj->BOTH : $db_obj->ASSOC;
    $r = @$db_obj->fetch_array($res, $result_type);
    return db_maybe_handle_error($r);
}

Happy to share more information on functions referenced in these two functions if you need them to get a better sense of how they work just lmk. My question is: how can these two functions be rewritten to so that they run without error and still use the existing concept that uses: stmt_init, prepare($q), execute and store_result. It's important that flow is mostly preserved how they are for other code segments in the code base.

Here are some of the various SE I've studied that are similar but have missed the mark for my questions:

Lot of up votes and shares good information about the error but doesn't exactly tell me where the free_result or close methods should be placed Commands out of sync; you can't run this command now

Another good one but no solid answers MySQL Error: Commands out of sync; you can't run this command now

Not good answers and don't want to use multiquery I get: Commands out of sync; you can't run this command now?

Not good answers, but this person's flow is similar Uncaught mysqli_sql_exception: Commands out of sync error

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • This is very similar to your last question. – Dharman Apr 21 '23 at 22:24
  • The problem isn't in this code. The problem is that you're calling this function while some other query is still in progress. You need to fetch all the results from that query before starting another one. – Barmar Apr 21 '23 at 22:25
  • Please edit this question and focus on a single problem: either the failing SQL is something you want to fix or you want to rewrite this code. Which is it? – Dharman Apr 21 '23 at 22:26
  • I am closing this question in favour of your previous one. I will edit the old one and reopen it. – Dharman Apr 21 '23 at 22:31
  • @Dharman understand and thank you. Will write back on there. – cricketplayer123 Apr 21 '23 at 22:35

0 Answers0