Example #1
0
function db_Connect()
{
    // Safely call this multiple times, only the first time has any effect //
    if (!db_IsConnected()) {
        global $db;
        // Connect to the database //
        $db = new mysqli(CMW_DB_HOST, CMW_DB_LOGIN, CMW_DB_PASSWORD, CMW_DB_NAME);
        // http://php.net/manual/en/mysqli.quickstart.connections.php
        if ($db->connect_errno) {
            db_Log("Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error);
        }
        // Set character set to utf8mb4 mode (default is utf8mb3 (utf8). mb4 is required for Emoji)
        $db->set_charset('utf8mb4');
        // More info: http://stackoverflow.com/questions/279170/utf-8-all-the-way-through
    }
}
Example #2
0
function _db_BindExecute(&$st, $args)
{
    if (count($args) > 0) {
        // Build the type string //
        $arg_types_string = "";
        foreach ($args as &$arg) {
            if (is_integer($arg)) {
                $arg_types_string .= 'i';
            } else {
                if (is_float($arg)) {
                    $arg_types_string .= 'd';
                } else {
                    if (is_string($arg)) {
                        $arg_types_string .= 's';
                    } else {
                        if (is_bool($arg)) {
                            $arg_types_string .= 'i';
                            $arg = $arg ? 1 : 0;
                        } else {
                            if (is_array($arg)) {
                                $arg_types_string .= 's';
                                $arg = json_encode($arg, true);
                            } else {
                                db_Log("Unable to parse " . gettype($arg));
                            }
                        }
                    }
                }
            }
        }
        $st->bind_param($arg_types_string, ...$args);
    }
    $GLOBALS['DB_QUERY_COUNT']++;
    $ret = $st->execute();
    if (!$ret || $st->errno) {
        db_LogError();
        $st->close();
        return false;
    }
    return true;
}