Custom MySQLi statement function for binding variables I have the following functions:

function db ($query, $parameters = false, $results = false, $fetch = false, $error_reporting = true) {

    global $db;
    global $db_query;

    if(!isset($db)) {

        $db = new mysqli("localhost", "username", "password", "db");

    }

    if ($db-&gt;connect_error == NULL) {

        $db-&gt;set_charset("utf8");

        if(is_array($parameters)) {

            $db_query = $db-&gt;prepare($query);

            if($db_query) {

                foreach($parameters as $key =&gt; $value) {
                    $parameters_reference[$key] = &amp;$parameters[$key];
                }

                $ReflectionMethod = new ReflectionMethod ('mysqli_stmt', 'bind_param');
                @$ReflectionMethod -&gt; invokeArgs($db_query, $parameters_reference);

                $execute = $db_query-&gt;execute();

                if($execute) {

                    if($results === false &amp;&amp; $fetch === false) {

                        $return = $db_query;

                    } else {

                        if($results === true) {
                            $return = $db_query-&gt;get_result();
                        }

                        if($results === true &amp;&amp; $fetch === true) {
                            $return = $return-&gt;fetch_array(MYSQLI_ASSOC);
                        }
                    }

                } else {
                    $db_error = "Execution error.";
                }

            } else {
                $db_error = "Preparation error.";
            }

        } else {

            $return = $db-&gt;query($query);

            if($parameters === true) {
                $return = $return-&gt;fetch_array(MYSQLI_ASSOC);;
            }

        }

    } else {
        $db_error = "Connection error.";
    }

    if(!isset($db_error)) {

        return $return;

    } else {

        if($error_reporting === true) {

            die("Database Error: ".$db_error);

        } else {

            return false;
        }
    }
}

function db_fetch($query) {
    return $query-&gt;fetch_array(MYSQLI_ASSOC);
}


Their main purpose is to save me time when executing queries to the database. The usage is as follows:

$query = db("SELECT * FROM `table`");


When I need to execute a simple query:

$query = db("SELECT * FROM `table` WHERE `var` = ?", ['i', $var], true);


When I need to add some variables to the query. Then I use $row = db_fetch($query) to get the results. It's possible to skip the db_fetch function if i set 4th parameter of the db function to true.

How can I make the functions better? I mean, is it written well or can I change something to make it better?
An obligatory suggestion: you can greatly improve any mysqli code by switching to PDO. So your function would be three times shorter 

function db ($query, $parameters = false)
{
    global $db;
    if(!isset($db))
    {
        $options = array(
            PDO::ATTR_ERRMODE            =&gt; PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE =&gt; PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES   =&gt; FALSE,
        );
        $dsn = 'mysql:host=localhost;dbname=db;charset=utf8';
        $db = new PDO($dsn, "username", "password", $options);
    }

    if (!$parameters)
    {
        return $db-&gt;query($query);
    } else {
        $stmt = $db-&gt;prepare($query);
        $stmt-&gt;execute($parameters);
        return $stmt;
    }
}


note that PDO has much better equivalent of your db_fetch() function, letting you to get the data simpler and in more formats than your awkward $results / $fetch combination:

$row = db("SELECT * FROM t WHERE id=?", [$id])-&gt;fetch();
$count = db("SELECT count(*) FROM t")-&gt;fetchColumn();
$data = db("SELECT name, id FROM t")-&gt;fetchAll(PDO::FETCH_KEY_PAIR);


compare these explicit calls where you always can tell what does your code return with your 

 $data = db("SELECT name, id FROM t", true, true);


show this code to someone and ask what's in $data. Or look at this code after a couple of months and try to tell it yourself without confusing all these true ans false parameters.