/** * sql execute * @param $sql * @param bool $id * @return bool|int|mysqli_result|string */ public function execute($sql, $id = false) { if (mysqli_query($this->handler, $sql)) { if ($id) { return mysqli_insert_id($this->handler); // return mysql_insert_id(); } return true; } else { TXLogger::addError(sprintf("sql Error: %s [%s]", mysqli_error($this->handler), $sql)); TXLogger::error($sql, 'sql Error:'); return false; } }
/** * 拼装orderby * @param $orderBy * @return string */ protected function buildOrderBy($orderBy) { $orders = array(); foreach ($orderBy as $key => $val) { $key = $this->real_escape_string($key); if (is_array($val)) { $asc = isset($val[0]) ? $val[0] : 'ASC'; $code = isset($val[1]) ? $val[1] : 'gbk'; if (!in_array(strtoupper($asc), array('ASC', 'DESC'))) { TXLogger::error("order must be ASC/DESC, {$asc} given", 'sql Error'); continue; } $orders[] = "CONVERT(`{$key}` USING {$code}) {$asc}"; } else { if (!in_array(strtoupper($val), array('ASC', 'DESC'))) { TXLogger::error("order must be ASC/DESC, {$val} given", 'sql Error'); continue; } $orders[] = '`' . $key . "` " . $val; } } if ($orders) { return ' ORDER BY ' . join(',', $orders); } else { return ''; } }
/** * 拼装Doubleorderby * @param $orderBys * @return string */ protected function buildOrderBy($orderBys) { $orders = array(); foreach ($orderBys as $k => $orderBy) { if (is_string($k) && in_array($k, $this->doubles)) { $table = $k; } else { if (isset($this->doubles[$k])) { $table = $this->doubles[$k]; } else { if (is_string($k)) { $k = $this->real_escape_string($k); //外层循环 if (is_array($orderBy)) { $asc = isset($orderBy[0]) ? $orderBy[0] : 'ASC'; $code = isset($orderBy[1]) ? $orderBy[1] : 'gbk'; if (!in_array(strtoupper($asc), array('ASC', 'DESC'))) { TXLogger::error("order must be ASC/DESC, {$asc} given", 'sql Error'); continue; } $orders[] = "CONVERT(`{$k}` USING {$code}) {$asc}"; } else { if (!in_array(strtoupper($orderBy), array('ASC', 'DESC'))) { TXLogger::error("order must be ASC/DESC, {$orderBy} given", 'sql Error'); continue; } $orders[] = '`' . $k . "` " . $orderBy; } continue; } else { continue; } } } foreach ($orderBy as $key => $val) { $key = $this->real_escape_string($key); if (is_array($val)) { $field = $table . ".`" . $key . '`'; $asc = isset($val[0]) ? $val[0] : 'ASC'; $code = isset($val[1]) ? $val[1] : 'gbk'; if (!in_array(strtoupper($asc), array('ASC', 'DESC'))) { TXLogger::error("order must be ASC/DESC, {$asc} given", 'sql Error'); continue; } $orders[] = "CONVERT({$field} USING {$code}) {$asc}"; } else { if (!in_array(strtoupper($val), array('ASC', 'DESC'))) { TXLogger::error("order must be ASC/DESC, {$val} given", 'sql Error'); continue; } $orders[] = $table . ".`" . $key . "` " . $val; } } } if ($orders) { return ' ORDER BY ' . join(',', $orders); } else { return ''; } }