|
Step 3: Creating Data Access Methods
<?php /** * Add a MP3 to the backend storage. * * @param string $title The title of the MP3. * @param string $author The interpret of the MP3. * @param string $album The album of the MP3. * @param array $tag The ID tag of the MP3. * * @return int The ID of the new MP3. */ function add($title, $author, $album, $tag = null) { /* Make sure we have a valid database connection. */ $this->_connect();
$id = $this->_db->nextId($this->_params['table']);
$query = sprintf( 'INSERT INTO %s (song_id, song_owner, song_title, ' . 'song_interpret, song_album, song_idtag) ' . 'VALUES (%d, %s, %s, %s, %s, %s)', $this->_params['table'], $id, $this->_db->quote(Auth::getAuth()), $this->_db->quote(String::convertCharset($title, NLS::getCharset(), $this->_params['charset'])), $this->_db->quote(String::convertCharset($author, NLS::getCharset(), $this->_params['charset'])), $this->_db->quote(String::convertCharset($album, NLS::getCharset(), $this->_params['charset'])), $this->_db->quote(serialize($tag)));
... }
/** * Retrieves one MP3 from the database. * * @param string $id The id of the MP3 to retrieve. * * @return array The array of MP3 attributes. */ function get($id) { ...
$row = $result->fetchRow(DB_FETCHMODE_ASSOC); if (is_a($row, 'PEAR_Error')) { return $row; }
/* Decode and return the MP3. */ $row['song_title'] = String::convertCharset($row['song_title'], $this->_params['charset']); $row['song_interpret'] = String::convertCharset($row['song_interpret'], $this->_params['charset']); $row['song_album'] = String::convertCharset($row['song_album'], $this->_params['charset']);
return $row; } ?>
|