Jump to content
MakeWebGames

PHP 5 MySql class system.


Recommended Posts

Okay; Today i needed a class file that would save me time while doing everyting i needed.

Its MySql only, and has examples for most function.

Replies would be great, ways to improve etc would also be appricated.

When inlcuded into a file, it doesnt need to be set up like McCodes, just use $db->blahh etc........

Nothing has been tested, any errors, I'll fix.

<?php
/*
Copywrite goes to Fusion
	Writen by Fusion
Please keep this, it doesnt affect the code so why not?
*/
class details {
const DB_HOST = 'localhost',
	  DB_USER = 'username',
	  DB_PASS = 'password',
	  DB_NAME = 'database';
}
$details = new details;

class database {
	var $connection_identifier;
	var $numb_queries;
	var $last_query;

		public function __construct($host, $user, $pass, $db) {
			$this->connection_identifier = mysql_connect($host, $user, $pass, 1) OR DIE('Error: Server connection failed.');
				mysql_select_db($db) OR DIE('Error: Database connection failed.');
					return $this->connection_identifier;
		}
		public function change_database($newdb) {
			return mysql_select_db($newdb);
		}
		public function execute($input) {
			$this->numb_queries++;
			$query = str_replace('<prefix>', '`'. $details->DB_NAME .'`.', $input);
				$this->last_query = mysql_query($query, $this->connection_identifier) OR $this->debug($query);
				return $this->last_query;
		}
		/*
				$userid = 4
			Example: $db->select('money', 'members', '`userid` = '. $userid .'');
			Output: SELECT `money` FROM <prefix>`members` WHERE (`userid` = 4);
		*/
		public function select($select, $table, $where) {
			$query = $this->execute("SELECT `". $select."` FROM <prefix>`". $table ."` WHERE (". $where .");");
				return ($this->num_rows($query)) ? $this->fetch($query, 'single') : 'N/A';
		}
		/*
				$deluserid = 4
			Example: $db->delete('members', '`userid` = '. $deluserid .'');
			Output: DELETE FROM <prefix>`members` WHERE (`userid` = 4);
		*/
		public function delete($table, $where) {
			$query = "DELETE FROM <prefix>`". $table ."` WHERE (". $where .");";
				return $this->execute($query);
		}
		/*	
				$_POST['username'] = 'bob'
				$_POST['email'] = '[email protected]'
				$_POST['password'] = 'bobloves666'
			Example: $db->insert('members', '`username`, `email`, `password`', '\''. $_POST['username'] .'\', \''. $_POST['email'] .'\', \''. $_POST['password'] .'\'');
			Output: INSERT INTO <prefix>`members` (`username`, `email`, `password`) VALUES ('bob', '[email protected]', 'bobloves666');
		*/
		public function insert($table, $columns, $values) {
			$columns = !empty($columns) ? '('. $columns .')' : '';
			$query = "INSERT INTO <prefix>`". $table ."` ". $columns ." VALUES (". $values .");";
				return $this->execure($query);
		}
		/*
				$userid = 4
			Example: $db->update('members', 'money', 'money', '+', '666', '`userid` = '. $userid .'');
			Output: UPDATE <prefix>`members` SET `money` = `money` + 666   WHERE (`userid` = 4);
		*/
		public function update($table, $what, $equal, $aORm, $value, $where) {
			$query = "UPDATE <prefix>`". $table ."` SET `". $what ."` = `". $equal ."` ". $aORm ." ". $value ." WHERE (". $where .");";
				return $this->execute($query);
		}
		/*
			Example: $db->truncate('members');
			Output: TRUNCATE TABLE <prefix>`members`;
		*/
		public function truncate($table) {
			$query = "TRUNCATE TABLE <prefix>`". $table ."`;";
				return $this->execute($query);
		}
		/*
			Fetch row example: $db->fetch($db->execute('somequery'), 'row');
			Fetch array example: $db->fetch($db->execute('somequery'), 'array');
			Fetch single example: $db->fetch($db->execute('somequery'), 'single');
		*/
			public function fetch($input, $fetchwhat = 'row') {
			if($fetchwhat == 'row') {
				return mysql_fetch_assoc($input);
			}
			else if($fetchwhat == 'array') {
				return mysql_fetch_array($input);
			}
			else if($fetchwhat == 'single') {
				return mysql_result($input, 0, 0);
			}
			}
		/*
			Fetch_object example: $db->object('somequery');
		*/
			public function object($input) {
				$input = (empty($input)) ? $this->last_query : $input;
					$execute = $this->execute($input);
						$object = mysql_fetch_object($execute);
							mysql_free_result($object);
					return $object;
			}
			private function debug($input) {
				exit('<p style="margin: 2px;">MySQL Error: '. mysql_error .'
Query was: '. $input .'</p>');
			}
			public function count() {
				return $this->numb_queries;
			}
			public function last_insert() {
				return mysql_insert_id();
			}
			public function escape_string($input) {
				return mysql_real_escape_string($input);
			}
			public function close() {	
				return mysql_close();
			}
}
		$db = new database($details->DB_HOST, $details->DB_USER, $details->DB_PASS, $details->DB_NAME);
	unset($details);
?>
Link to comment
Share on other sites

Not exactly how I would do it, also, your functions do not take care of securing the SQL, which is potentially dangerous.

For example I would tend to try to use MySQLi for new PHP 5 projects and not the old API, and your update function is quiet limited as it allows only one type of updates...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...