PHP5 Database Class

PHP

MIT License

Simple class for connecting to and using a MySQL database.

Download (right click, save as, rename as appropriate)

Embed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// $db = new Database(array(host, user, password));
class Database {

	private $db;
	private $connection;
	private	$query;

	public function Database($args = array()) {
		$this->db['host']	= $args['host'];
		$this->db['user']	= $args['user'];
		$this->db['pass']	= $args['pass'];
	}

	public function connect() {
		$this->connection = mysqli_connect($this->db['host'], $this->db['user'], $this->db['pass']);
		if (!$this->connection) {
			$this->_add("Connection Error: " . mysqli_connect_error(), 2);
			return false;
		};
	return true; }

	public function disconnect() {
		if ($this->connection) {
			mysqli_close($this->connection);
			$this->connection = null;
			return true;
		} else {
			$this->_add("Connection Error: Unable to disconnect, not connected to '" . $this->db['host'] ."'", 2);
		};
	return false; }

	public function select_db($e) {
		if (mysqli_select_db($this->connection, $e)) {
			return true;
		} else {
			$this->_add("Connection Error: Unable to select the database", 2);
		};
	return false; }

	public function query($sql) {
		$this->query = $sql;
		if ($result = mysqli_query($this->connection, $sql)) {
			return $result;
		} else {
			$this->_add("SQL Error: The following query was not successful '" . $this->query . "'", 2);
		};
	return false; }

	public function clean($dirty) {
		if (!is_array($dirty)) {
			$dirty = mysqli_real_escape_string($this->connection, trim($dirty));
			$clean = stripslashes($dirty);
	return $clean; };
		$clean = array();
		foreach ($dirty as $p => $data) {
			$data = mysqli_real_escape_string($this->connection, trim($data));
			$data = stripslashes($data);
			$clean[$p] = $data; };
	return $clean; }

}