mysqli.php 1.56 KB
Newer Older
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
<?php
class Db_Mysqli implements IDb {
	private $link;

	function connect($host, $user, $pass, $db, $port) {
		if ($port)
			$this->link = mysqli_connect($host, $user, $pass, $db, $port);
		else
			$this->link = mysqli_connect($host, $user, $pass, $db);

		if ($this->link) {
			$this->init();

			return $this->link;
		} else {
			die("Unable to connect to database (as $user to $host, database $db): " . mysqli_error());
		}
	}

	function escape_string($s, $strip_tags = true) {
		if ($strip_tags) $s = strip_tags($s);

		return mysqli_real_escape_string($this->link, $s);
	}

	function query($query, $die_on_error = true) {
		$result = mysqli_query($this->link, $query);
		if (!$result) {
			user_error("Query $query failed: " . ($this->link ? mysqli_error($this->link) : "No connection"),
				$die_on_error ? E_USER_ERROR : E_USER_WARNING);
		}

		return $result;
	}

	function fetch_assoc($result) {
		return mysqli_fetch_assoc($result);
	}


	function num_rows($result) {
		return mysqli_num_rows($result);
	}

	function fetch_result($result, $row, $param) {
		if (mysqli_data_seek($result, $row)) {
			$line = mysqli_fetch_assoc($result);
			return $line[$param];
		} else {
			return false;
		}
	}

	function close() {
		return mysqli_close($this->link);
	}

	function affected_rows($result) {
		return mysqli_affected_rows($this->link);
	}

	function last_error() {
		return mysqli_error();
	}

	function init() {
		$this->query("SET time_zone = '+0:0'");

		if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
			$this->query("SET NAMES " . MYSQL_CHARSET);
		}

		return true;
	}

}
?>