* @version 1.0 * @package skyNomad */ /** * DB_Engine * @package skyNomad * @subpackage classes */ class db_engine { # --- Variables --- # Public /** * Contains the MySQL Host * @access public * @var string */ public $mysql_host; /** * Contains the MySQL Username * @access public * @var string */ public $mysql_user; /** * Contains the MySQL Password * @access public * @var string */ public $mysql_pass; /** * Contains the MySQL Database to acces * @access public * @var string */ public $mysql_db; # Private /** * Contains the Database Handler * @access private * @var mysql_connection */ private $link; /** * Contains the Logging Engine * @access private * @var global_log */ private $logger; /** * Contains the Database Connection Status * @access private * @var string */ private $status; /** * Sets whether or not to turn Debuggin on * @access private * @var string */ private $debug; # --- Functions --- /** * Constructor sets up {$mysql_host, $mysql_user, $mysql_pass, $mysql_db, $debug} */ function db_engine($mysql_host="", $mysql_user="", $mysql_pass="", $mysql_db="", $debug="0"){ # Initialise Variables $this->mysql_host = $mysql_host; $this->mysql_user = $mysql_user; $this->mysql_pass = $mysql_pass; $this->mysql_db = $mysql_db; $this->debug = $debug; $this->logger = 0; } /** * Connects to the MySQL database and sets up the database handler. */ function db_connect(){ # Create Connection To Database if ($this->mysql_host && $this->mysql_user && $this->mysql_pass){ $this->link = mysql_connect($this->mysql_host, $this->mysql_user, $this->mysql_pass); } else if ($this->mysql_host && $this->mysql_user && !($tyhis->mysql_pass)){ $this->link = mysql_connect($this->mysql_host, $this->mysql_user); } else if ($this->mysql_host && !($this->mysql_user) && !($tyhis->mysql_pass)){ $this->link = mysql_connect($this->mysql_host); } # Handle any errors $this->err_handler(mysql_error()); # Set Database if ($this->mysql_db){ mysql_select_db($this->mysql_db, $this->link); } # Handle any errors $this->err_handler(mysql_error()); } /** * Connects to the database, executes a query, returns the result * if needed, and closes the connection. * @param string $query The SQL query to execute. * @return mysql_result */ function query($query){ # Connect To Database $this->db_connect(); # Execute SQL Command $result = mysql_query($query); # Handle Errors $this->err_handler(mysql_error()); # Return Result return $result; } /** * Handles any errors that are generated by MySQL. If $debug is set * to true, then the error will be displayed. * @param mysql_error $err The MySQL Error object. */ function err_handler($err){ if ($err) { if ($this->debug){ # Output error print "
DB Error: " . $err . "
\n"; die(); } } } /** * Updates the data of a row in a table. * @param string $table The table name to update. * @param array $data An array with the new values. * @param array $id The index of the row to update. */ function update_table($table, $data, $id) { # Construct Update Query $query = "UPDATE `$table`"; $x = 0; foreach ($data as $d){ if ($x == 0){ $query .= "SET "; $query .= "`" . $d[0] . "` = '" . $d[1] . "' "; } else { $query .= ", `" . $d[0] . "` = '" . $d[1] . "'"; } $x++; } $query .= " WHERE `" . $id[0] . "` = '" . $id[1] . "'"; # Execute Query $this->query($query); } /** * Sets the MySQL Host */ function set_mysql_host($data){ $this->mysql_host = $data; } /** * Sets the MySQL Host */ function set_logger($logger){ $this->logger = $logger; } /** * Sets the MySQL Username */ function set_mysql_user($data){ $this->mysql_user = $data; } /** * Sets the MySQL Password */ function set_mysql_pass($data){ $this->mysql_pass = $data; } /** * Sets the MySQL Database */ function set_mysql_db($data){ $this->mysql_db = $data; } /** * Turns debuggin mode on or off */ function set_debug($debug){ $this->debug = $debug; } } ?>