<?php
require_once(__DIR__."/../config/config.inc.php");
$mysqli = new mysqli(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME);
if (mysqli_connect_errno()) {
    echo mysqli_connect_error();
    exit();
}
$mysqli->query("SET NAMES 'utf8mb4';");

class Custom_MySQLi_Result
{
    private $data;
    private $position = 0;
    public $num_rows;

    public function __construct($data)
    {
        $this->data = $data;
        $this->num_rows = count($data);
    }

    public function fetch_array($mode = MYSQLI_ASSOC)
    {
        if ($this->position < $this->num_rows) {
            $row = $this->data[$this->position];
            $this->position++;
            return $row;
        }
        return null;
    }
}

function custom_get_result($stmt)
{
    $stmt->store_result();
    $meta = $stmt->result_metadata();

    if (!$meta) return false;

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

    $stmt->bind_result(...$params);

    $result = [];
    while ($stmt->fetch()) {
        $c = [];
        foreach ($row as $key => $val) {
            $c[$key] = $val;
        }
        $result[] = $c;
    }

    $stmt->free_result();

    return new Custom_MySQLi_Result($result);
}