/** * 执行SQL * * @access public * @param mixed $sql * @return mixed */ public function doSql($sql) { //更好的处理表前缀 //$mainStr = ltrim(self::$dbinfo['tablePre'],'tiny_'); $sql = preg_replace('/(\\s+|,)(tiny_)+/i', '$1' . self::$dbinfo['tablePre'], $sql); $sql = trim($sql); if (DEBUG) { $doSqlBeforTime = microtime(true); $result = mysql_query($sql, self::$conn); $useTime = microtime(true) - $doSqlBeforTime; Tiny::setSqlLog($sql, $useTime); } else { $result = mysql_query($sql, self::$conn); } //查询不成功时返回空数组 $rows = array(); //分析出读写操作 if (preg_match("/^(select|show)(.*)/i", $sql) == 0) { if ($result) { if (stripos($sql, 'insert') !== false) { return $this->lastId(); } else { if (stripos($sql, 'update') !== false) { return mysql_affected_rows(); } } return $result; } return false; } else { if (is_resource($result)) { while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $rows[] = $row; } mysql_free_result($result); return $rows; } else { if (DEBUG) { throw new Exception("SQLError:{$sql} ," . mysql_error() . "", E_USER_ERROR); } } return array(); } }