db = FALSE; $this->prepared = ''; $this->useQueryCache = $useQueryCache; $this->queryCacheSize=$queryCacheSize; } function _enableQueryCache() { //do we have query cache? //set cache size return FALSE; } function _disableQueryCache() { return FALSE; } /** * @return bool TRUE upon successful connection * @param string $host * @param string $user * @param string $password * @param string $database */ function connect($host, $user, $password, $database) { if( ( $db = mysql_connect( $host, $user, $password ) ) === FALSE ) { //failed to connect return FALSE; } if( mysql_select_db( $database, $db ) === FALSE ) { //failed to select $database return FALSE; } $this->db =& $db; if( $this->useQueryCache) { $this->_enableQueryCache(); } return TRUE; } /** * @return void */ function disconnect() { if( $this->isConnected() ) { mysql_close($this->db); $this->db = FALSE; } } /** * @return bool TRUE if connected */ function isConnected() { return $this->db !== FALSE; } /** * Fetches a row or a single value from database. * @return mixed an array or scalar or FALSE on error * @param string $sql * @param mixed $args array */ function selectOne( $sql, $args='', $FILE='', $LINE='' ) { if( $args && !is_array($args) ) { $args = array($args); } if( $args ) { $sql = $this->escapeString($sql, $args); } if( ($res = mysql_query($sql, $this->db)) === FALSE ) { //query failed! trigger_error(mysql_error() . $FILE . $LINE, E_USER_WARNING); return FALSE; } if( mysql_num_rows($res) == 0 ) { return NULL; // no matches } $row = mysql_fetch_assoc($res); if( sizeof( $row ) > 1 ) { return DBWrapper::array_stripslashes($row); # array } list($value) = array_values($row); return DBWrapper::array_stripslashes($value); } /** * @return mixed array of rows or NULL if no matches or FALSE on error * @param string $sql * @param mixed $args array of values */ function selectRows( $sql, $args='', $FILE='', $LINE='') { if( $args ) { if( !is_array($args)){ $args = array($args); } } if( is_array($args) ) { $sql = $this->escapeString( $sql, $args ); } if( ($res = mysql_query($sql, $this->db)) === FALSE ) { trigger_error(mysql_error() . $FILE . $LINE, E_USER_WARNING); return FALSE; } if( mysql_num_rows($res) == 0 ) { return NULL; } $data = array(); while( $row = mysql_fetch_assoc($res)) { $data[] = $row; } mysql_free_result($res); return $data; } function selectColumn($sql, $args='', $FILE='', $LINE='') { if( $args ) { if( !is_array($args)){ $args = array($args); } } if( is_array($args) ) { $sql = $this->escapeString( $sql, $args ); } if( ($res = mysql_query($sql, $this->db)) === FALSE ) { trigger_error(mysql_error() . $FILE . $LINE, E_USER_WARNING); return FALSE; } if( mysql_num_rows($res) == 0 ){ return null; } $rows = array(); while( $row = mysql_fetch_row($res) ) { $rows[] = $row[0]; } return $rows; } /** * Perform an sql query (non-select) * @return mixed number of rows affected or FALSE on error * @param string $sql * @param mixed $args single value or ordered array */ function execute( $sql, $args='', $FILE='', $LINE='' ) { if( $args && !is_array($args) ) { $args = array($args); } if( $args ) { $sql = $this->escapeString( $sql, $args ); } if( mysql_query($sql, $this->db) === FALSE ) { trigger_error(mysql_error() . " on $FILE at $LINE", E_USER_WARNING); return FALSE; } return mysql_affected_rows($this->db); } /** * Prepare a statement for execution w/ executeStatement() */ function prepare($sql) { $this->prepared = $sql; } /** * Execute a prepared statement. * @param mixed $args scalar or ordered array * @return int num rows affected or FALSE on error */ function executePrepared($args) { if( !$this->prepared ) { trigger_error( "Cannot execute because I don't have a prepared statement" ); return FALSE; } if( !is_array($args) ) { $args = array($args); } return $this->execute($this->prepared, $args); } /** * @return mixed string message of last error of FALSE if no error */ function lastError() { if( $this->isConnected()) { return mysql_error($this->db); } return FALSE; //no error } function lastInsertID() { return mysql_insert_id( $this->db); } /** * @return int num rows affected by last query */ function affectedRows() { if( $this->isConnected()) { return mysql_affected_rows($this->db); } return 0; } /** * @access private * @return string mysql real escaped string, also add 's around values * @todo BAD '?' is a valid SQL string used in searches! Solution is * to make that part of query be one of the $args */ function escapeString( $sql, $args ) { if(!is_array($args)) $args = array($args); $sqlArr = explode( '?', $sql, sizeof($args) ); if( sizeof($sqlArr) != sizeof($args)) { trigger_error( "mismatch error: $sql {$args[0]}", E_USER_WARNING ); return FALSE; } ksort($args); $sqlArr = explode( '?', $sql ); for( $i=0; $idb); } } ?>