Location: PHPKode > scripts > Impleo Music Collection Script > impleo/classes/Database.class.php
<?php
/* ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <hide@address.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return Christoffer Kjeldgaard Petersen
 * ----------------------------------------------------------------------------
 */

/**
 * Database class for communicating with a MySQL server. Implemented as singleton pattern.
 */
class Database
{
	private static $instance; // Object instance
	private $dbConnection; // Holds database connection
	
	// Database info:
	const HOST = "localhost";
	const DBUSER = "root";
	const DBPASS = "";
	const DBNAME = "impleo";
	
    const TABLE_START = "im_"; // Prefix of tables in the database.

	/**
	 * Constructor. Establishes connection to the database.
	 */
	private function __construct()
	{
		if (!$this->dbConnection = @mysql_connect(self::HOST, self::DBUSER, self::DBPASS))
		{
			trigger_error('Could not connect to database', E_USER_ERROR);
		}
		else if (!@mysql_select_db(self::DBNAME, $this->dbConnection))
		{
			trigger_error('Could not select database', E_USER_ERROR);
		}
	}

    /**
     * Gets the prefix of tables in the database.
     * @return string Prefix
     */
    public static function getTableStart()
    {
        return self::TABLE_START;
    }
	
	/**
	 * Queries to the database.
	 *
	 * @param string $sql SQL
	 * @return int Last inserted ID.
	 */
	public function query($sql)
	{
		mysql_query($sql, $this->dbConnection) or die(mysql_error());
		return mysql_insert_id();
	}
	
	/**
	 * Takes a SQL SELECT statement and fetches the results to objects.
	 *
	 * @param string $sql The SQL SELECT Statement.
	 * @return array An array of associative result arrays.
	 */
	public function fetch($sql)
	{
		if (strtoupper(substr($sql, 0, 6)) != "SELECT")
			trigger_error('Only SELECT statements allowed', E_USER_ERROR);
		if (!$rs = mysql_query($sql, $this->dbConnection))
			trigger_error('Query failed', E_USER_ERROR);

		if (mysql_num_rows($rs))
		{
			$arrResult = array();
			while ($obj = mysql_fetch_assoc($rs))
				$arrResult[] = $obj;
			return $arrResult;
		}
		return NULL;
	}

	/**
	 * Handles SQL SELECT statements for getting a single row from the database.
	 *
	 * @param string $sql SQL SELECT Statement.
	 * @return array The result as associative array
	 */
	public function fetchRow($sql)
	{
		if (strtoupper(substr($sql, 0, 6)) != "SELECT")
			trigger_error('Only SELECT statements allowed', E_USER_ERROR);
		if (!$rs = mysql_query($sql, $this->dbConnection))
			trigger_error('Query failed', E_USER_ERROR);

		if (mysql_num_rows($rs) == 1)
			return mysql_fetch_assoc($rs);
		else
			return NULL;
	}

    /**
     * Run SHOW-sql on the database.
     *
     * @param sql $sql SQL SHOW statement
     * @return bool Returns true if exactly one row was found, false otherwise
     */
    public function show($sql)
    {
        if (strtoupper(substr($sql, 0, 4)) != "SHOW")
			trigger_error('Only SHOW statements allowed', E_USER_ERROR);
		if (!$rs = mysql_query($sql, $this->dbConnection))
			trigger_error('Query failed', E_USER_ERROR);

        if (mysql_num_rows($rs) == 1)
            return true;
        else
            return false;
    }
	
	/**
	 * Escapes special characters in a string for use in a SQL statement
	 *
	 * @param string Unescaped string
	 * @return Escaped string
	 */
	public function escapeString($str)
	{
		return mysql_real_escape_string($str);
	}

	/**
	 * Singleton method
	 *
	 * @return Database Instance of class Database.
	 */
	public static function getInstance()
	{
		if (!self::$instance instanceof self)
		{ 
			self::$instance = new self;
		}
		return self::$instance;
	}

	/**
	 * Destructor. Closes connection to the database.
	 */
	function __destruct()
	{
		@mysql_close($this->dbConnection);
    }
}

?>
Return current item: Impleo Music Collection Script