* @copyright Copyright © 2006 Peter Adams * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0 * @category owa * @package owa * @version $Revision$ * @since owa 1.0.0 */ class owa_db_mysql extends owa_db { function connect() { if (!$this->connection) { if ($this->getConnectionParam('persistant')) { $this->connection = mysql_pconnect( $this->getConnectionParam('host'), $this->getConnectionParam('user'), $this->getConnectionParam('password'), $this->getConnectionParam('open_new_connection') ); } else { $this->connection = mysql_connect( $this->getConnectionParam('host'), $this->getConnectionParam('user'), $this->getConnectionParam('password'), $this->getConnectionParam('open_new_connection') ); } $this->database_selection = mysql_select_db($this->getConnectionParam('name'), $this->connection); if (function_exists('mysql_set_charset')) { mysql_set_charset('utf8',$this->connection); } else { $this->query("SET NAMES 'utf8'"); } } if (!$this->connection || !$this->database_selection) { $this->e->alert('Could not connect to database.'); $this->connection_status = false; return false; } else { $this->connection_status = true; return true; } } /** * Database Query * * @param string $sql * @access public * */ function query($sql) { if ($this->connection_status == false): owa_coreAPI::profile($this, __FUNCTION__, __LINE__); $this->connect(); owa_coreAPI::profile($this, __FUNCTION__, __LINE__); endif; owa_coreAPI::profile($this, __FUNCTION__, __LINE__); $this->e->debug(sprintf('Query: %s', $sql)); $this->result = ''; $this->new_result = ''; if (!empty($this->new_result)): mysql_free_result($this->new_result); endif; owa_coreAPI::profile($this, __FUNCTION__, __LINE__, $sql); $result = @mysql_unbuffered_query($sql, $this->connection); owa_coreAPI::profile($this, __FUNCTION__, __LINE__); // Log Errors if (mysql_errno($this->connection)): $this->e->debug(sprintf('A MySQL error occured. Error: (%s) %s. Query: %s', mysql_errno($this->connection), htmlspecialchars(mysql_error($this->connection)), $sql)); endif; owa_coreAPI::profile($this, __FUNCTION__, __LINE__); $this->new_result = $result; return $this->new_result; } function close() { @mysql_close($this->connection); return; } /** * Fetch result set array * * @param string $sql * @return array * @access public */ function get_results($sql) { if ($sql): $this->query($sql); endif; $num_rows = 0; while ( $row = @mysql_fetch_assoc($this->new_result) ) { $this->result[$num_rows] = $row; $num_rows++; } if ($this->result): return $this->result; else: return null; endif; } /** * Fetch Single Row * * @param string $sql * @return array */ function get_row($sql) { $this->query($sql); //print_r($this->result); $row = @mysql_fetch_assoc($this->new_result); return $row; } /** * Prepares and escapes string * * @param string $string * @return string */ function prepare($string) { if ($this->connection_status == false): $this->connect(); endif; return mysql_real_escape_string($string, $this->connection); } } ?>