host = $_host; } else { if (defined('DB_HOST')) { $this->host = DB_HOST; } else { die('MySQL Host not defined.'); } } if (isset($_username)) { $this->username = $_username; } else { if (defined('DB_USERNAME')) { $this->username = DB_USERNAME; } else { die('MySQL Username not defined.'); } } if (isset($_password)) { $this->password = $_password; } else { if (defined('DB_PASSWORD')) { $this->password = DB_PASSWORD; } else { die('MySQL Password not defined.'); } } if (isset($_database)) { $this->database = $_database; } else { if (defined('DB_DATABASE')) { $this->database = DB_DATABASE; } else { die('MySQL Database not defined.'); } } $this->connect(); } private function connect() { $this->connection_id = mysql_pconnect($this->host, $this->username, $this->password); mysql_select_db($this->database, $this->connection_id); } public function close() { mysql_close($this->connection_id); } public function query($query = null) { $this->query = (!empty($query)) ? $query : $this->build_query(); $this->clear_query(); $this->result = mysql_query($this->query, $this->connection_id); $this->error = mysql_error(); if ($this->error && $this->debug) { header('content-type: text/plain'); print "MySQL Error: {$this->error}. In query:\n"; print $this->query; exit; } } /** * Escape table or field name strinning some * unsupported characters * * @param string $_field * @return string */ private function escape_field_name($_field) { $needle = array('`', '\\', '/', '\'', '"'); $_field = str_replace($needle, null, $_field); return $_field; } /** * Check fields for type and existense * Used in insert and update functions * * @param string $_table * @param array $_data array(field => value) * @return Array of unsupported fields */ private function check_fields($_table, $_data) { $invalid_fields = array(); $fields = mysql_list_fields($this->database, $_table); $num_fields = mysql_num_fields($fields); for($i = 0; $i < $num_fields; $i++) { $field_exists = false; $valid = &$field_exists; $field_name = mysql_field_name($fields, $i); if (in_array($field_name, array_keys($_data))) { $field_exists = true; $field_type = mysql_field_type($fields, $i); if ($field_type == 'int' && !is_numeric($_data[$field_name])) { $valid = false; } } if ($valid) unset($_data[$field_name]); } return array_keys($_data); } /** * Fetch names of fields in table * * @param string $table * @return Array of field names */ public function fetch_fields($table) { $result = array(); $fields = mysql_list_fields($this->database, $table); $columns = mysql_num_fields($fields); for($i = 0; $i < $columns; $i++) { $result[] = mysql_field_name($fields, $i); } return $result; } /** * Obtain resource link * * @param string|mysql_query $query * @return MySQL resource */ public function fetch_resource($query = null) { $this->query($query); return $this->result; } public function fetch_dbda($class_name, $cut = true, $query = null) { if (!class_exists($class_name, false) && $this->debug) { die('DBDA class "' . $class_name . '" do not exists.'); } $ids = $this->fetch_list($query); $result = array(); foreach ($ids as $id) { $result[] = new $class_name($id); } return (count($result) == 1 && $cut) ? $result[0] : $result; } public function fetch_object() { $arg_list = func_get_args(); if (count($arg_list) == 2) { $query = $arg_list[0]; $cut = $arg_list[1]; } elseif (is_string($arg_list[0])) { $query = $arg_list[0]; $cut = true; } elseif (is_bool($arg_list[0])) { $query = ''; $cut = $arg_list[0]; } else { $query = ''; $cut = true; } $result = array(); $this->query($query); while ($row = mysql_fetch_object($this->result)) { array_push($result, $row); } return (count($result) == 1 && $cut) ? $result[0] : $result; } public function fetch_array() { $arg_list = func_get_args(); if (count($arg_list) == 3) { $query = $arg_list[0]; $cut = $arg_list[1]; $assoc = $arg_list[2]; } elseif (count($arg_list) == 2 && is_string($arg_list[0])) { $query = $arg_list[0]; $cut = $arg_list[1]; } elseif (count($arg_list) == 2 && is_bool($arg_list[0])) { $cut = $arg_list[0]; $assoc = $arg_list[1]; } elseif (isset($arg_list[0]) && is_string($arg_list[0])) { $query = $arg_list[0]; } elseif (isset($arg_list[0]) && is_bool($arg_list[0])) { $cut = $arg_list[0]; } if (!isset($query)) $query = ''; if (!isset($cut)) $cut = true; if (!isset($assoc)) $assoc = true; $result = array(); $array_type = ($assoc) ? MYSQL_ASSOC : MYSQL_NUM; $this->query($query); while ($row = mysql_fetch_array($this->result, $array_type)) { array_push($result, $row); } return (count($result) == 1 && $cut) ? $result[0] : $result; } public function fetch_value($query = null) { $this->query($query); if ($this->result) { $result = mysql_fetch_row($this->result); } return (isset($result[0])) ? $result[0] : false; } public function fetch_list($query = null) { $data = $this->fetch_array($query, false, false); $result = array(); foreach ($data as $k => $v) { $result[$k] = $v[0]; } return $result; } public function &select() { $arg_list = func_get_args(); foreach ($arg_list as $arg) { $this->select[] = $arg; } return $this; } public function &from() { $arg_list = func_get_args(); foreach ($arg_list as $arg) { $this->from[] = $arg; } return $this; } public function &where() { $arg_list = func_get_args(); $last_key = ''; $or_and = 'AND'; for ($i = 0; $i < count($arg_list); $i++) { if ($arg_list[$i] == 'OR' && $i % 2 == 0) { $or_and = 'OR'; $i--; array_shift($arg_list); continue; } if ($i % 2 == 0) { if (preg_match('[(.+)\s(.+$)]', $arg_list[$i], $m)) { $last_key = $m[1]; $operator = $m[2]; } else { $last_key = $arg_list[$i]; $operator = '='; } } else { $this->where[] = array($last_key, $arg_list[$i], $operator, $or_and); $operator = '='; $or_and = 'AND'; } } return $this; } public function &order_by() { $arg_list = func_get_args(); foreach ($arg_list as $arg) { $this->order[] = $arg; } return $this; } public function &limit($arg1, $arg2 = false) { $this->limit[0] = intval($arg1); if ($arg2 !== false) $this->limit[1] = intval($arg2); return $this; } public function build_query() { $sql = 'SELECT ' . implode(', ', $this->select); $sql .= ' FROM ' . implode(', ', $this->from); if (!empty($this->where)) { $sql .= ' WHERE '; foreach ($this->where as $key => $condition) { if ($key) $sql .= " {$condition[3]} "; $sql .= "`{$condition[0]}` {$condition[2]} '{$condition[1]}'"; } } if (!empty($this->order)) { $sql .= ' ORDER BY ' . implode(', ', $this->order); } if (!empty($this->limit)) { $sql .= " LIMIT {$this->limit[0]}"; if (isset($this->limit[1])) $sql .= ", {$this->limit[1]}"; } return $sql; } private function clear_query() { $this->select = array(); $this->from = array(); $this->where = array(); $this->order = array(); $this->limit = array(); } /** * Check is value present in table * * @param string $table * @param string $field * @param string|int|float $value * @return bool */ public function in_table($table, $field, $value) { $value = $this->select($field)->from($table)->where($field, $value)->fetch_value(); return ($value === false) ? false : true; } /** * Get single field * * @param string $table * @param string $field * @param array|string $index array(field, value) or just value * @return Field value or boolean false */ public function get_value($table, $field, $index) { if (!is_array($index)) { $index = array($this->index_field, $index); } $result = $this->select($field)->from($table)->where($index[0], $index[1]); $result = $this->fetch_value(); return $result; } /** * Update row * * All fields are verefied before update. * If field not exists or mistmatch by type it would be skiped. * All values are escaped using mysql_real_escape_string(). * * Index field always is in exceptions array. * * @param string $_table * @param array $_data array(field => value) * @param array|string $_index array(field, value) or only value * @param array|string $_exceptions array or string with fields delimeted by comma * @return Void */ public function update($_table, $_data, $_index, $_exceptions = array()) { if (!is_array($_index)) { $_index = array($this->index_field, $_index); } $query = "UPDATE\n\t`$_table`\nSET\n"; if (is_string($_exceptions)) $_exceptions = explode(',', $_exceptions); $_exceptions[] = $_index[0]; $invalid_fields = $this->check_fields($_table, $_data); $fields = array(); foreach ($_data as $f => $v) { if (!in_array($f, $invalid_fields) && !in_array($f, $_exceptions)) { if (get_magic_quotes_gpc()) $v = stripslashes($v); $fields[] = "`$f` = '" . mysql_real_escape_string($v) . "'"; } } $query .= implode(', ', $fields); $query .= "\nWHERE\n\t`{$_index[0]}` = '{$_index[1]}'"; $this->query($query); } /** * Insert new row into a table * * All fields are verefied before update. * If field not exists or mistmatch by type it would be skiped. * All values are escaped using mysql_real_escape_string(). * * If exceptions === true function will return LAST_INSERT_ID * and no exeptions will be used. * * @param string $_table * @param array $_data array(field => value) * @param array|string $_exceptions array or string with fields delimeted by comma * @param bool $_return_id default is false * @return Void or LAST_INSERT_ID */ public function insert($_table, $_data, $_exceptions = array(), $_return_id = false) { $_table = $this->escape_field_name($_table); if ($_exceptions === true) { $_return_id = true; $_exceptions = array(); } $query = "INSERT INTO\n\t`$_table`\nSET\n"; if (is_string($_exceptions)) $_exceptions = explode(':', $_exceptions); if (is_string($_data)) { list($field, $value) = split('::', $_data); $_data = array($field => $value); } $invalid_fields = $this->check_fields($_table, $_data); $fields = array(); foreach ($_data as $f => $v) { if (!in_array($f, $invalid_fields) && !in_array($f, $_exceptions)) { if (get_magic_quotes_gpc()) $v = stripslashes($v); $fields[] = "`$f` = '" . mysql_real_escape_string($v) . "'"; } } $query .= implode(', ', $fields); $this->query($query); if ($_return_id) { return $this->fetch_value('SELECT LAST_INSERT_ID()'); } } /** * Delete row by index * * @param string $_table * @param array|string $_index array(field, value) or only value */ public function delete($_table, $_index) { if (!is_array($_index)) { $_index = array($this->index_field, $_index); } $_table = $this->escape_field_name($_table); $_index[0] = $this->escape_field_name($_index[0]); $_index[1] = mysql_real_escape_string($_index[1]); $this->query("DELETE FROM `$_table` WHERE `{$_index[0]}` = '{$_index[1]}'"); } } ?>