Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Database class.
- * Class to interact with the database for the program.
- */
- class Database {
- ##########################################################
- /* Database Details */
- private $database_server; // database server, provided by user.
- private $database_username; // database username, provided by user.
- private $database_password; // database password, provided by user, can be blank.
- private $database_database; // database name, provided by user
- /* Program Cache */
- private $query; // stores the last query resource here.
- private $error; // stores the program error here. get_error() retrieves it for public use.
- ##########################################################
- /**
- * init function.
- * Inits the class, attempts to connect and select the database.
- * @access public
- * @param array $database_details
- * @return bool
- */
- public function init($database_details) {
- /* Checks to see if the data we got was in the form of an array. */
- if(!is_array($database_details)) {
- $this->error = "Database details must be in the form of an array.";
- return false;
- }
- /* Checks to see if any values were empty.. other then password.. */
- if(empty($database_details["server"]) || empty($database_details["username"]) || empty($database_details["database"])) {
- $this->error = "One of the required database details was left blank.";
- return false;
- }
- /* Lets set the details now to the class */
- $this->database_server = $database_details["server"];
- $this->database_username = $database_details["username"];
- $this->database_password = $database_details["password"];
- $this->database_database = $database_details["database"];
- /* Now we need to try and connect to the MySQL server, if it isn't good, we stop here. */
- if(!$this->connect_mysql()) {
- return false;
- }
- /* Now we need to try and select the database from the MySQL server, if it doesn't turn back good, stop. */
- if(!$this->select_database()) {
- return false;
- }
- // passed.
- return true;
- }
- ########################
- # Connection Functions #
- ########################
- /**
- * connect_mysql function.
- * Attempts to connect to MySQL with user provided details.
- * @access private
- * @return bools
- */
- private function connect_mysql() {
- /* This below will try and connect to the MySQL server with the details. */
- $connnect_resource = @mysql_connect($this->database_server, $this->database_username, $this->database_password);
- /* If it didn't work, we need to get the error from MySQL, and stop */
- if(!$connect_resource) {
- $this->error = mysql_error();
- return false;
- }
- return true; // it seems to have worked!
- }
- /**
- * select_db function.
- * Attempts to select a database from the MySQL server with the provided details.
- * @access private
- * @return bool
- */
- private function select_db() {
- /* This will try and select the database from the MySQL server. */
- $select_resource = @mysql_select_db($this->database_database);
- /* If it didn't go successfully, we need to get the error from MySQL and stop here. */
- if(!$select_resource) {
- $this->error = mysql_error();
- return false;
- }
- return true; // selected it successfully! :)
- }
- ###################
- # Query functions #
- ###################
- /**
- * query function.
- * Executes a provided query on the MySQL server and stores and returns it.
- * @access public
- * @param string $query_text
- * @return mysql_query_resource
- */
- public function query($query_text) {
- /* We must have some sort of query to run.. checks it. */
- if(empty($query_text)) {
- $this->error = "Query must have some text to execute";
- return false;
- }
- $query = @mysql_query($query_text); // execute the query.
- $this->query = $query; // store the resource in this class.
- return $query; // return the resource.
- }
- /**
- * num_rows function.
- * Numbers the rows returned on a given query.
- * @access public
- * @param mysql_query_resource $query_resource. (default: '')
- * @return mysql_num_rows_resource
- */
- public function num_rows($query_resource = '') {
- $query_resource = ($query_resource = '') ? $this->query : $query_resource; // if the query resource is blank, use last query.
- $num_rows = @mysql_num_rows($query_resource); // execute it..
- return $num_rows; // return the num_rows resource.
- }
- /**
- * fetch_object function.
- * Fetch's an object with the data from a query
- * @access public
- * @param mysql_query_resource $query_resource
- * @return mysql_object_resource
- */
- public function fetch_object($query_resource = '') {
- $query_resource = ($query_resource = '') ? $this->query : $query_resource; // if the query resource is blank, use last query.
- $fetch_object = @mysql_fetch_object($query_resource); // fetch it..
- return $fetch_object; // return the object.
- }
- /**
- * fetch_array function.
- * Fetch's an array with the data from a query.
- * @access public
- * @param mysql_query_resource $query_resource. (default: '')
- * @return mysql_array_resource
- */
- public function fetch_array($query_resource = '') {
- $query_resource = ($query_resource = '') ? $this->query : $query_resource; // if the query resource is blank, use last query.
- $fetch_array = @mysql_fetch_array($query_resource); // fetch it..
- return $fetch_array; // return the array.
- }
- /**
- * count_table function.
- * Counts up all the rows in a table. Faster then just mysql_num_rows() the table.
- * @access public
- * @param string $table_name
- * @return int
- */
- public function count_table($table_name) {
- /* Checks to make sure the table name is not empty. */
- if(empty($table_name)) {
- $this->error = "You need a table name to count!"; // sets the program error.
- return false;
- }
- $this->query("SELECT COUNT(*) FROM `$table_name`"); // execute the count query.. faster then just numing all the rows.
- $count = $this->mysql_fetch_array(); // gets the array for the count.
- return $count[0]; // return the count.
- }
- ###################
- # Class Functions #
- ###################
- /**
- * get_error function.
- * Get's the error from the class, if any.
- * @access public
- * @return string
- */
- public function get_error() {
- return $this->error;
- }
- }
Add Comment
Please, Sign In to add comment