host = $host; $this->dbUser = $dbUser; $this->dbPass = $dbPass; $this->dbName = $dbName; $this->connectToDb(); } /** * Internal command to connect to the database * @return void */ private function connectToDb () { if (!$this->dbConn = @pmb_mysql_connect($this->host, $this->dbUser, $this->dbPass)) { $this->handleError(); return false; } else if ( !@pmb_mysql_select_db($this->dbName, $this->dbConn) ) { $this->handleError(); return false; } } /** * Internal command to handle errors and populate the error variable * @return void */ private function handleError () { $this->error = pmb_mysql_error($this->dbConn); } /** * Command which runs queries. Returns a class on success and flase on error * @param string $sql The SQL query to run * @return class * @uses lastfmApiDatabase_result */ public function query($sql) { if ( !$queryResource = pmb_mysql_query($sql, $this->dbConn) ) { echo pmb_mysql_error(); $this->handleError(); return false; } else { $return = new lastfmApiDatabase_result($this, $queryResource); return $return; } } } /** * A class which allows interaction with results when a query is run by lastfmApiDatabase * @package base */ class lastfmApiDatabase_result { /** * Stores the mysql class * @var class */ var $mysql; /** * Stores the query * @var class */ var $query; /** * Run when the class is created. Sets up the variables * @param class $mysql The mysql class * @param class $query The query * @return void */ public function lastfmApiDatabase_result(&$mysql,$query) { $this->mysql = &$mysql; $this->query = $query; } /** * Fetches the next result * @return array */ public function fetch () { if ( $row = pmb_mysql_fetch_array($this->query) ) { return $row; } else if ( $this->size() > 0 ) { pmb_mysql_data_seek($this->query,0); return false; } else { return false; } } /** * Fetches all the results * @return array */ public function fetchAll() { $result = array(); while ( $row = pmb_mysql_fetch_array($this->query) ) { $result[] =$row; } return $result; } /** * Shows the number of results * @return integer */ public function size () { return pmb_mysql_num_rows($this->query); } } ?>