* @version 0.5.2 * @package Katharsis */ class Katharsis_Db5 { /** * @var string */ const FETCHMODE_ASSOC = 'ASSOC'; /** * @var string */ const FETCHMODE_ARRAY = 'ARRAY'; /** * @var Mysql Resource */ private $_connection = null; /** * @var string */ private $_host = null; /** * @var string */ private $_user = null; /** * @var string */ private $_password = null; /** * @var string */ private $_database = null; /** * @var string */ private $_lastStatement = ""; /** * @var array */ private $_lastResult = array(); /** * @var array */ private $_lastError = array(); /** * @var int */ private $_lastRowCount = 0; /** * Create a connection * * @param $host * @param $user * @param $password * @param $database * * @return KatharsisDbConnector */ public function __construct($host = null, $user = null, $password = null, $database = null) { $this->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) . "'"; } $sets[] = "`" . $key . "` = " . $value; } $sql = 'INSERT INTO ' . $table; if($values !== array()) { $sql .= ' SET ' . implode(',', $sets); } else { $sql .= ' () VALUES () '; } 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); } public function fetchField ($statement, $field = null) { if($field === null) { $result = $this->_fetch($statement, self::FETCHMODE_ARRAY, true); return $result[0]; } else { $result = $this->_fetch($statement, self::FETCHMODE_ASSOC, true); if(array_key_exists($field, $result)) { return $result[$field]; } return 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'] . ' |