setHost($host); $this->setUser($user); $this->setPassword($password); $this->setDatabase($database); if($host !== null && $user !== null && $password !== null && $database !== null) { $this->connect(); } } /** * Sets the value of $_host * * @param string $host * @author Karl Pannek */ public function setHost($value) { $this->_host = $value; } /** * Sets the value of $_user * * @param string $user * @author Karl Pannek */ public function setUser($value) { $this->_user = $value; } /** * Sets the value of $_password * * @param string $password * @author Karl Pannek */ public function setPassword($value) { $this->_password = $value; } /** * Sets the value of $_database * * @param string $database * @author Karl Pannek */ public function setDatabase($value) { $this->_database = $value; $this->_selectDatabase(); } /** * Returns the value of $_host * * @return string * @author Karl Pannek */ public function getHost() { return $this->_host; } /** * Returns the value of $_user * * @return string * @author Karl Pannek */ public function getUser() { return $this->_user; } /** * Returns the value of $_password * * @return string * @author Karl Pannek */ public function getPassword() { return $this->_password; } /** * Returns the value of $_database * * @return string * @author Karl Pannek */ public function getDatabase() { return $this->_database; } /** * Connect to database * * * @return void */ public function connect() { $this->_connection = mysql_connect( $this->getHost(), $this->getUser(), $this->getPassword(), true ); if(!$this->_connection) { throw new KatharsisDb5_Exception('Could not connect to "' . $this->getHost() . '" with user "' . $this->getUser() . '".'); } $this->_selectDatabase(); } /** * Disconnect database connection * @return bool */ public function disconnect() { $this->_connection = null; return (bool) mysql_close($this->_connection); } /** * Checks connection to database * * @return bool */ public function isConnected() { if($this->_connection !== null && $this->_connection !== false) { return true; } else { return false; } } /** * Returns mysql connection link resource * * @return mysql link resource */ public function getMysqlResource () { return $this->_connection; } /** * Executes a Sql statement * * @param $statement * @return bool */ public function run ($statement) { if($this->_execute($statement)) { return true; } else { return false; } } /** * Returns Result set for incremental fetching * * @param $statement * @return KatharsisDb5_ResultSet */ public function runForIncrementalFetch ($statement) { $resultSet = $this->_execute($statement); return new KatharsisDb5_ResultSet($resultSet); } /** * Inserts a row into a specified table * * @param $table * @param $values * @return bool */ public function insert ($table, $values = array()) { $sets = array(); foreach($values as $key => $value) { if(is_string($value)) { $value = "'" . mysql_real_escape_string($value, $this->_connection) . "'"; } if($value === null) { $value = 'NULL'; } $sets[] = "`" . $key . "` = " . $value; } $sql = 'INSERT INTO ' . $table; if($values !== array()) { $sql .= ' SET ' . implode(',', $sets); } else { $sql .= ' () VALUES () '; } return $this->run($sql); } public function simpleDelete($table, $fieldvalue, $fieldname = 'id') { $sql = "DELETE FROM " . $table . " WHERE " . $fieldname . " = :field"; $sql = $this->createStatement($sql, array('field' => $fieldvalue)); return $this->run($sql); } /** * Executes Query and returns number of rows * * @param $statement * @return int */ public function count ($statement) { $result = $this->_execute($statement); $this->_lastRowCount = mysql_num_rows($result); return $this->_lastRowCount; } /** * Returns a fetched result set (All rows) * * @param $statement * @param $fetchmode * @return array */ public function fetchAll($statement, $fetchmode = self::FETCHMODE_ASSOC) { return $this->_fetch($statement, $fetchmode, false); } /** * Returns a fetched result (One rows) * * @param $statement * @param $fetchmode * @return array */ public function fetchOne($statement, $fetchmode = self::FETCHMODE_ASSOC) { return $this->_fetch($statement, $fetchmode, true); } /** * Returns a fetched result (One rows) * * @param $statement * @param $fetchmode * @return array */ public function fetchField ($statement, $field = 0) { if(intval($field) === $field) { $result = $this->_fetch($statement, self::FETCHMODE_ARRAY, true); } else { $result = $this->_fetch($statement, self::FETCHMODE_ASSOC, true); } return array_key_exists($field, $result) ? $result[$field] : null; } /** * Prints out details of the last query (Html formatted) * * @return void */ public function analyseLast () { $debug = debug_backtrace(); $file = explode("/", str_replace("\\", "/", $debug[0]['file'])); $file = $file[count($file)-1]; echo '
'; echo '
QUERY ANALYSIS | from ' . $file . ' on line ' . $debug[0]['line'] . ' | '; echo ''; echo ' |
'; print_r((string) $this->_lastStatement); echo '
'; echo ''; print_r($this->_lastResult); echo '
'; echo ''; echo '
Number: | ' . $this->_lastError['number'] . ' |
Message: | ' . $this->_lastError['message'] . ' |