db::query PHP Method

query() static public method

You can use any valid MySQL query here. This is also the fallback method if you can't use one of the provided shortcut methods from this class.
static public query ( string $sql, boolean $fetch = true ) : mixed
$sql string The sql query
$fetch boolean True: apply db::fetch to the result, false: go without db::fetch
return mixed
    static function query($sql, $fetch = true)
    {
        $connection = self::connect();
        if (error($connection)) {
            return $connection;
        }
        // save the query
        self::$last_query = $sql;
        // execute the query
        $result = @mysql_query($sql, $connection);
        self::$affected = @mysql_affected_rows();
        self::$trace[] = $sql;
        if (!$result) {
            return self::error(l::get('db.errors.query_failed', 'The database query failed'));
        }
        if (!$fetch) {
            return $result;
        }
        $array = array();
        while ($r = self::fetch($result)) {
            array_push($array, $r);
        }
        return $array;
    }

Usage Example

Example #1
0
 /**
  * Send key to db
  */
 public function send_key_to_db()
 {
     if (!self::$flag_key_sent_to_db) {
         $db = new db($this->db_link);
         // todo: disable logging in db
         $db->query("SELECT set_config('sm.numbers.crypt.key', '" . $db->escape($this->key) . "', false)");
         $db->query("SELECT set_config('sm.numbers.crypt.options', '" . $db->escape($this->cipher) . "', false)");
         // todo: enable logging in db
         self::$flag_key_sent_to_db = true;
     }
     return true;
 }
All Usage Examples Of db::query