Free database class file

Jake

Paragon
Joined
Nov 14, 2010
Messages
2,264
Reaction score
2
FP$
246
Hey guys, quite awhile ago I wrote a PHP database class. It basically just assists you in connecting and querying your database a bit easier in PHP using mySQL or mySQLi.

Here is the class contents:
Code:
<?php

class database {

	private $host;
	private $user;
	private $pass;
	private $db;
	private $connector;
	public  $connection;


	function __construct($host, $user, $pass, $db, $connector='mysql') {
		$this->host      = $host;
		$this->user      = $user;
		$this->pass      = $pass;
		$this->db        = $db;
		$this->connector = strtolower($connector);
	}


	public function openConnection() {
		try {

			if ($this->connector == 'mysql') {
				$this->connection = mysql_connect($this->host,$this->user,$this->pass);
				if (mysql_errno()) {
					printf('Failed to connect: %s', mysql_error());
				} else {
					mysql_select_db($this->db, $this->connection);
				}
			}

			elseif ($this->connector == 'mysqli') {
				$this->connection = mysqli_connect($this->host,$this->user,$this->pass);
				if (mysqli_errno($this->connection)) {
					printf('Failed to connect: %s', mysqli_error());
				} else {
					mysqli_select_db($this->connection, $this->db);
				}
			}

		} catch (exception $e) {
			return $e;
		}
	}


	public function closeConnection() {
		try {

			if ($this->connector == 'mysql') {
				mysql_close();
			}

			elseif ($this->connector == 'mysqli') {
				mysqli_close($this->connection);
			}

		} catch (exception $e) {
			return $e;
		}
	}

	public function slashString($string) {
		try {

			return addslashes($string);

		} catch (exception $e) {
			return $e;
		}
	}

	public function escapeString($string) {
		try {

			if ($this->connector == 'mysql') {
				return mysql_real_escape_string($string);
			}

			elseif ($this->connector == 'mysqli') {
				return mysqli_real_escape_string($this->connection,$string);
			}

		} catch (exception $e) {
			return $e;
		}
	}


	public function queryDB($query) {
		try {

			if ($this->connector == 'mysql') {
				return mysql_query($query);
			}

			elseif ($this->connector == 'mysqli') {
				return mysqli_query($this->connection,$query);
			}

		} catch (exception $e) {
			return $e;
		}
	}

	public function dbNumRows($query) {
		try {

			if ($this->connector == 'mysql') {
				return mysql_num_rows($query);
			}

			elseif ($this->connector == 'mysqli') {
				return mysqli_num_rows($query);
			}

		} catch (exception $e) {
			return $e;
		}
	}

	public function fetchAssoc($toFetch) {
		try {

			if($this->connector == 'mysql') {
				return mysql_fetch_assoc($toFetch);
			}

			elseif($this->connector == 'mysqli') {
				return mysqli_fetch_assoc($toFetch);
			}
		} catch (exception $e) {
			return $e;
		}
	}
	
	public function fetchArray($toFetch) {
		try {

			if ($this->connector == 'mysql') {
				return mysql_fetch_array($toFetch);
			}

			elseif ($this->connector == 'mysqli') {
				return mysqli_fetch_array($toFetch);
			}

		} catch (exception $e) {
			return $e;
		}
	}
}
?>

Basically, what you do is create a new PHP file and:

Code:
$connect = new database(hostname, username, password, dbname, connector);
$connect->openConnection();
$connect->queryDB("sql");
$connect->closeConnection();

And that's about it!

You can see the github here: https://github.com/JakeA/DatabaseClass
 
Back
Top Bottom