2013-04-17 09:08:36 +00:00
|
|
|
<?php
|
|
|
|
class Db_Mysql implements IDb {
|
|
|
|
private $link;
|
2016-04-26 17:04:24 +00:00
|
|
|
private $last_error;
|
2013-04-17 09:08:36 +00:00
|
|
|
|
|
|
|
function connect($host, $user, $pass, $db, $port) {
|
|
|
|
$this->link = mysql_connect($host, $user, $pass);
|
2013-04-17 13:00:24 +00:00
|
|
|
|
2013-04-17 09:08:36 +00:00
|
|
|
if ($this->link) {
|
|
|
|
$result = mysql_select_db($db, $this->link);
|
|
|
|
if (!$result) {
|
|
|
|
die("Can't select DB: " . mysql_error($this->link));
|
|
|
|
}
|
2013-04-17 11:36:34 +00:00
|
|
|
|
|
|
|
$this->init();
|
|
|
|
|
2013-04-17 09:08:36 +00:00
|
|
|
return $this->link;
|
|
|
|
} else {
|
|
|
|
die("Unable to connect to database (as $user to $host, database $db): " . mysql_error());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function escape_string($s, $strip_tags = true) {
|
2013-04-17 14:56:13 +00:00
|
|
|
if ($strip_tags) $s = strip_tags($s);
|
|
|
|
|
2013-04-17 09:08:36 +00:00
|
|
|
return mysql_real_escape_string($s, $this->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
function query($query, $die_on_error = true) {
|
2013-08-02 12:20:39 +00:00
|
|
|
$result = @mysql_query($query, $this->link);
|
2013-04-17 09:08:36 +00:00
|
|
|
if (!$result) {
|
2016-04-26 17:04:24 +00:00
|
|
|
$this->last_error = @mysql_error($this->link);
|
2013-08-02 12:20:39 +00:00
|
|
|
|
|
|
|
@mysql_query("ROLLBACK", $this->link);
|
2016-04-26 17:04:24 +00:00
|
|
|
user_error("Query $query failed: " . ($this->link ? $this->last_error : "No connection"),
|
2013-04-17 12:05:52 +00:00
|
|
|
$die_on_error ? E_USER_ERROR : E_USER_WARNING);
|
2013-04-17 09:08:36 +00:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetch_assoc($result) {
|
|
|
|
return mysql_fetch_assoc($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function num_rows($result) {
|
|
|
|
return mysql_num_rows($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetch_result($result, $row, $param) {
|
|
|
|
return mysql_result($result, $row, $param);
|
|
|
|
}
|
|
|
|
|
|
|
|
function close() {
|
|
|
|
return mysql_close($this->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
function affected_rows($result) {
|
|
|
|
return mysql_affected_rows($this->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
function last_error() {
|
2013-04-17 14:56:13 +00:00
|
|
|
return mysql_error();
|
2013-04-17 09:08:36 +00:00
|
|
|
}
|
|
|
|
|
2016-04-26 17:04:24 +00:00
|
|
|
function last_query_error() {
|
|
|
|
return $this->last_error;
|
|
|
|
}
|
|
|
|
|
2013-04-17 10:23:35 +00:00
|
|
|
function init() {
|
|
|
|
$this->query("SET time_zone = '+0:0'");
|
|
|
|
|
|
|
|
if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
|
|
|
|
$this->query("SET NAMES " . MYSQL_CHARSET);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-17 09:08:36 +00:00
|
|
|
}
|
|
|
|
?>
|