'mysql_query', 'mysql_connect' => 'mysql_connect', 'mysql_pconnect' => 'mysql_pconnect', 'mysql_select_db' => 'mysql_select_db', 'mysql_num_rows' => 'mysql_num_rows', 'mysql_fetch_array' => 'mysql_fetch_array', 'mysql_real_escape_string' => 'mysql_real_escape_string', 'mysql_insert_id' => 'mysql_insert_id', 'mysql_fetch_assoc' => 'mysql_fetch_assoc', 'mysql_free_result' => 'mysql_free_result', 'mysql_error' => 'mysql_error', 'mysql_affected_rows' => 'mysql_affected_rows', 'mysql_get_server_info' => 'mysql_get_server_info', 'mysql_close' => 'mysql_close' ); protected $_mysqli = array( 'mysql_query' => 'mysqli_query', 'mysql_connect' => 'mysqli_connect', 'mysql_pconnect' => 'mysqli_pconnect', 'mysql_select_db' => 'mysqli_select_db', 'mysql_num_rows' => 'mysqli_num_rows', 'mysql_fetch_array' => 'mysqli_fetch_array', 'mysql_real_escape_string' => 'mysqli_real_escape_string', 'mysql_insert_id' => 'mysqli_insert_id', 'mysql_fetch_assoc' => 'mysqli_fetch_assoc', 'mysql_free_result' => 'mysqli_free_result', 'mysql_error' => 'mysqli_error', 'mysql_affected_rows' => 'mysqli_affected_rows', 'mysql_get_server_info' => 'mysqli_get_server_info', 'mysql_close' => 'mysqli_close' ); public function initizlize() { if($this->mysqli) $this->_cmd = $this->_mysql; else $this->_cmd = $this->_mysqli; $this->_connected = $this->_connect($this->host, $this->user, $this->password, $this->port, $this->persistent = false); if(!$this->_connected) $this->showError("Can't connect to database: '".$this->_sqlError()."'."); if(!@($this->_cmd['mysql_select_db'] == 'mysqli_select_db' ? $this->_cmd['mysql_select_db']($this->_connected, $this->name) : $this->_cmd['mysql_select_db']($this->name, $this->_connected))) $this->showError("Can't select database: '".$this->_sqlError()."'"); $this->query("Set names " . $this->charset); return true; } public function getVersion() { return @$this->_cmd['mysql_get_server_info']($this->_connected); } public function query($sql, &$link = '') { if(!$link) $link =& $this->_connected; $res = @($this->_cmd['mysql_query'] == 'mysqli_query' ? $this->_cmd['mysql_query']($link, $sql) : $this->_cmd['mysql_query']($sql, $link)); if(!$res) $this->showError("Query error: '".$this->_sqlError()."'."); return $res; } public function escape($param) { if(is_array($param)) return array_map(array(&$this, 'escape'), $param); if(get_magic_quotes_gpc()) $param = stripslashes($param); $param = @($this->_cmd['mysql_real_escape_string'] == 'mysqli_real_escape_string' ? $this->_cmd['mysql_real_escape_string']($this->_connected, $param) : $this->_cmd['mysql_real_escape_string']($param)); return $param; } public function getLastId() { return @$this->_cmd['mysql_insert_id']($this->_connected); } public function freeResult() { if(is_resource($this->rQuery)) @$this->_cmd['mysql_free_result']($this->rQuery); } public function close() { if($this->_connected) return @$this->_cmd['mysql_close']($this->_connected); } protected function _getRow($sql, $assoc, &$link) { $res = $this->query($sql, $link); $res = $this->_cmd['mysql_fetch_array']($res, ($assoc ? MYSQL_ASSOC : MYSQL_NUM)); return $res ? $res : array(); } protected function _getRows($sql, $assoc = true, &$link) { $rows = array(); $assoc = ($assoc ? MYSQL_ASSOC : MYSQL_NUM); $this->rQuery = $this->query($sql, $link); while($row = $this->_cmd['mysql_fetch_array']($this->rQuery, $assoc)) $rows[] = $row; return $rows; } private function _connect($host, $user, $pass, $port = false, $persistent = false) { if($port) $host = $host . ':' . $port; if($link = ($persistent ? @$this->_cmd['mysql_pconnect']($host, $user, $pass) : @$this->_cmd['mysql_connect']($host, $user, $pass))) return $link; return false; } private function _sqlError() { return ($this->_cmd['mysql_error'] == 'mysqli_error' ? @$this->_cmd['mysql_error']($this->_connected) : @$this->_cmd['mysql_error']()); } private function showError($msg) { echo $msg; die; } } class MysqlHelper { protected $_query = array(); protected $_words = array(); protected $_data = array(); public function getRows($sSql, $bAssoc = true) { return $this->_getRows($sSql, $bAssoc, $this->_bConnected); } public function getRow($sSql, $bAssoc = true) { return $this->_getRow($sSql, $bAssoc, $this->_bConnected); } public function getField($sSql) { return $this->_getField($sSql, $this->_bConnected); } public function select($sSelect) { if(!isset($this->_query['select'])) $this->_query['select'] = 'SELECT '; $this->_query['select'] .= $sSelect; return $this; } public function where($aConds) { $this->_query['where'] = ''; if(is_array($aConds) && count($aConds)) { foreach ($aConds as $sValue) $this->_query['where'] .= $sValue . ' '; $this->_query['where'] = "WHERE " . trim(preg_replace("/^(AND|OR)(.*?)/i", "", trim($this->_query['where']))); } else { if (!empty($aConds)) $this->_query['where'] .= 'WHERE ' . $aConds; } return $this; } public function from($sTable, $sAlias = '') { $this->_query['table'] = 'FROM ' . $sTable . ($sAlias ? ' AS ' . $sAlias : ''); return $this; } public function order($sOrder) { if (!empty($sOrder)) $this->_query['order'] = 'ORDER BY ' . $sOrder; return $this; } public function group($sGroup) { $this->_query['group'] = 'GROUP BY ' . $sGroup; return $this; } public function having($sHaving) { $this->_query['having'] = 'HAVING ' . $sHaving; return $this; } public function leftJoin($sTable, $sAlias, $mParam = null) { $this->_join('LEFT JOIN', $sTable, $sAlias, $mParam); return $this; } public function innerJoin($sTable, $sAlias, $mParam = null) { $this->_join('INNER JOIN', $sTable, $sAlias, $mParam); return $this; } public function join($sTable, $sAlias, $mParam = null) { $this->_join('JOIN', $sTable, $sAlias, $mParam); return $this; } public function limit($iPage, $sLimit = null, $iCnt = null) { if($sLimit === null && $iCnt === null && $iPage !== null) { $this->_query['limit'] = 'LIMIT ' . $iPage; return $this; } if($iCnt === null) $iOffset = $iPage; else { if($sLimit) { $iPages = ceil($iCnt / $sLimit); $iPage = max(1, min($iPages, $iPage)); $iOffset = $sLimit*($iPage-1); } else $iOffset = 0; } $this->_query['limit'] = ($sLimit ? 'LIMIT ' . $sLimit : '') . ($iOffset ? ' OFFSET ' . $iOffset : ''); return $this; } public function execute($sType = null, $aParams = array()) { $sSql = $this->_query['select'] . "\n"; $sSql .= $this->_query['table'] . "\n"; $sSql .= (isset($this->_query['join']) ? $this->_query['join'] . "\n" : ''); $sSql .= (isset($this->_query['where']) ? $this->_query['where'] . "\n" : ''); $sSql .= (isset($this->_query['group']) ? $this->_query['group'] . "\n" : ''); $sSql .= (isset($this->_query['having']) ? $this->_query['having'] . "\n" : ''); $sSql .= (isset($this->_query['order']) ? $this->_query['order'] . "\n" : ''); $sSql .= (isset($this->_query['limit']) ? $this->_query['limit'] . "\n" : ''); $sSql .= '/* OO Query */'; if(method_exists($this, '_execute')) $sSql = $this->_execute(); $this->_query = array(); $bDoCache = false; $sType = strtolower($sType); switch($sType) { case 'getrow': $aRows = $this->getRow($sSql); break; case 'getrows': $aRows = $this->getRows($sSql); break; case 'getfield': $aRows = $this->getField($sSql); break; case 'getslavefield': $aRows = $this->getSlaveField($sSql); break; default: return $sSql; break; } if(isset($aParams['free_result'])) { $this->freeResult(); } return $aRows; } public function insert($sTable, $aValues = array(), $bEscape = true, $bReturnQuery = false) { if (!$aValues) $aValues = $this->_data; $sValues = ''; foreach ($aValues as $mValue) if (is_null($mValue)) $sValues .= "NULL, "; else $sValues .= "'" . ($bEscape ? $this->escape($mValue) : $mValue) . "', "; $sValues = rtrim(trim($sValues), ','); if($this->_data) $this->_data = array(); $sSql = $this->_insert($sTable, implode(', ', array_keys($aValues)), $sValues); if($hRes = $this->query($sSql)) { if($bReturnQuery) return $sSql; return $this->getLastId(); } return 0; } public function clean() { $this->_query = array(); } public function update($sTable, $aValues = array(), $sCond = null, $bEscape = true) { if(!is_array($aValues) && count($this->_data)) { $sCond = $aValues; $aValues = $this->_data; $this->_data = array(); } $sSets = ''; foreach ($aValues as $sCol => $sValue) { $sCmd = "="; if (is_array($sValue)) { $sCmd = $sValue[0]; $sValue = $sValue[1]; } $sSets .= "{$sCol} {$sCmd} " . (is_null($sValue) ? 'NULL' : ($bEscape ? "'" . $this->escape($sValue) . "'" : $sValue)) . ", "; } $sSets[strlen($sSets)-2] = ' '; return $this->query($this->_update($sTable, $sSets, $sCond)); } public function delete($sTable, $sQuery, $iLimit = null) { if ($iLimit !== null) $sQuery .= ' LIMIT ' . (int) $iLimit; return $this->query("DELETE FROM {$sTable} WHERE ". $sQuery); } public function dropTables($aDrops, $aVals = array()) { foreach ($aDrops as $sDrop) $this->query("DROP TABLE {$sDrop}"); } protected function _join($sType, $sTable, $sAlias, $mParam = null) { if(!isset($this->_query['join'])) $this->_query['join'] = ''; $this->_query['join'] .= $sType . " " . $sTable . " AS " . $sAlias; if(is_array($mParam)) { $this->_query['join'] .= "\n\tON("; foreach($mParam as $sValue) $this->_query['join'] .= $sValue . " "; } else { if(preg_match("/(AND|OR|=)/", $mParam)) $this->_query['join'] .= "\n\tON({$mParam}"; else $this->showError("Not allowed to use 'USING()' in SQL queries any longer."); } $this->_query['join'] = preg_replace("/^(AND|OR)(.*?)/i", "", trim($this->_query['join'])) . ")\n"; } protected function _insert($sTable, $sFields, $sValues) { return 'INSERT INTO ' . $sTable . ' '.' (' . $sFields . ')'.' VALUES (' . $sValues . ')'; } protected function _update($sTable, $sSets, $sCond) { return 'UPDATE ' . $sTable . ' SET ' . $sSets . ' WHERE ' . $sCond; } private function _getField($sSql, &$hLink) { $sRes = ''; $aRow = $this->getRow($sSql, false, $hLink); if($aRow) $sRes = $aRow[0]; return $sRes; } }