diff --git a/classes/db.php b/classes/db.php
index 6dc31a22e065808923c1dd5dcf3397867cc65cf3..c9d9ad5ea9326bded4afb2e3cda7e040ddc1ed57 100644
--- a/classes/db.php
+++ b/classes/db.php
@@ -7,7 +7,11 @@ class Db implements IDb {
 	private function __construct() {
 		switch (DB_TYPE) {
 		case "mysql":
-			$this->adapter = new Db_Mysql();
+			if (function_exists("mysqli_connect")) {
+				$this->adapter = new Db_Mysqli();
+			} else {
+				$this->adapter = new Db_Mysql();
+			}
 			break;
 		case "pgsql":
 			$this->adapter = new Db_Pgsql();
@@ -16,7 +20,7 @@ class Db implements IDb {
 			die("Unknown DB_TYPE: " . DB_TYPE);
 		}
 
-		$this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT);
+		$this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : false);
 	}
 
 	private function __clone() {
diff --git a/classes/db/mysql.php b/classes/db/mysql.php
index fe5d05e2f540de0d245f30d168bb83b9507fe2b1..aab05aca239a13b4c0ac26c25e7a342d25a758df 100644
--- a/classes/db/mysql.php
+++ b/classes/db/mysql.php
@@ -20,6 +20,8 @@ class Db_Mysql implements IDb {
 	}
 
 	function escape_string($s, $strip_tags = true) {
+		if ($strip_tags) $s = strip_tags($s);
+
 		return mysql_real_escape_string($s, $this->link);
 	}
 
@@ -54,7 +56,7 @@ class Db_Mysql implements IDb {
 	}
 
 	function last_error() {
-		return mysql_affected_rows($this->link);
+		return mysql_error();
 	}
 
 	function init() {
diff --git a/classes/db/mysqli.php b/classes/db/mysqli.php
new file mode 100644
index 0000000000000000000000000000000000000000..e82f6630007fed0f211c00c20fff1bbb84e955b1
--- /dev/null
+++ b/classes/db/mysqli.php
@@ -0,0 +1,74 @@
+<?php
+class Db_Mysqli implements IDb {
+	private $link;
+
+	function connect($host, $user, $pass, $db, $port) {
+		$this->link = mysqli_connect($host, $user, $pass, $db, $port);
+
+		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;
+	}
+
+}
+?>
diff --git a/install/index.php b/install/index.php
index 1aae5da831ffad68ef0eab81c0212a1a4d519747..99339aca24ff48ee50648f55bd1299d1c89bea56 100644
--- a/install/index.php
+++ b/install/index.php
@@ -44,7 +44,7 @@
 			array_push($errors, "PHP support for JSON is required, but was not found.");
 		}
 
-		if ($db_type == "mysql" && !function_exists("mysql_connect")) {
+		if ($db_type == "mysql" && !function_exists("mysql_connect") && !function_exists("mysqli_connect")) {
 			array_push($errors, "PHP support for MySQL is required for configured $db_type in config.php.");
 		}