-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.php
154 lines (146 loc) · 5.12 KB
/
database.php
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
//////////////////////////////////////////////////////////////////////////////
// Разрабочик dmitry@protopopov.ru
require_once( dirname(__FILE__) . '/configuration.php' );
require_once( dirname(__FILE__) . '/factory.php' );
class JDatabase {
private $config;
var $last_query;
var $result;
var $mysqli;
var $num_queries=0;
public function __construct() {
$this->config = JFactory::getConfig();
}
protected static $instance;
public static function getInstance()
{
// Only create the object if it doesn't exist.
if (empty(self::$instance))
{
self::$instance = new JDatabase;
}
return self::$instance;
}
function connect()
{
if($this->config->persistent)
{
// http://habrahabr.ru/post/129482/
$this->mysqli=new mysqli('p:' . $this->config->host, $this->config->user, $this->config->password) or $this->connection_error();
}
else
{
$this->mysqli=new mysqli($this->config->host, $this->config->user, $this->config->password) or $this->connection_error();
}
$this->mysqli->select_db($this->config->db);
$this->mysqli->set_charset('utf8');
return $this->mysqli;
}
function disconnect()
{
if($this->mysqli) { $this->mysqli->close(); $this->mysqli=0; return 1; }
else { return 0; }
}
// The query must consist of a single SQL statement.
function execute($query,$params=array()){
if($this->config->debug) echo "<pre>$query</pre>";
$res = $this->mysqli->prepare($query);
if(!$res) $this->query_error();
$code = array();
$index=0; foreach($params as $param) {
$code[] = '$var'. $index .'=$params[' . $index . ']?$params[' . $index . ']:"";';
$index++;
}
if (strnatcmp(phpversion(),'5.4') < 0) $code[] = '$res->bind_param("' . str_repeat('s',$index) . '",&$var' . implode(',&$var',range(0,$index-1)) . ');';
if (strnatcmp(phpversion(),'5.4') >= 0) $code[] = '$res->bind_param("' . str_repeat('s',$index) . '",$var' . implode(',$var',range(0,$index-1)) . ');';
if($this->config->debug) echo implode(PHP_EOL,$code);
if($index) eval(implode(PHP_EOL,$code));
$res->execute();
$res->close();
}
function query($query)
{
if($this->config->debug) echo "<pre>$query</pre>";
$this->last_query=$query;
$this->num_queries++;
$this->result=$this->mysqli->query($this->last_query) or $this->query_error();
return $this->result;
}
function multi_query($query)
{
if($this->config->debug) echo "<pre>$query</pre>";
$this->last_query=$query;
$this->num_queries++;
$this->result=$this->mysqli->multi_query($this->last_query) or $this->query_error();
return $this->result;
}
// Returns an associative array of strings representing the fetched row in the result set, where each key in the array represents the name of one of the result set's columns or NULL if there are no more rows in resultset.
function fetch_row($result=0)
{
if(!$result) { $result=$this->result; }
return $result->fetch_assoc();
}
function fetch_all_rows($result=0)
{
if(!$result) { $result=$this->result; }
$rows = array(); while($row=$result->fetch_assoc()) $rows[] = $row;
return $rows;
}
// Returns the number of rows in the result set.
function num_rows($result=0)
{
if(!$result) { $result=$this->result; }
return $result->num_rows;
}
// Frees the memory associated with the result.
//Note
//You should always free your result with mysqli_free_result, when your result object is not needed anymore.
function free_result($result=0)
{
if(!$result) $result=$this->result;
if($result) $result->close();
}
function free_multi_result($result=0)
{
if(!$result) { $result=$this->result; }
if($result) do
if ($result = $this->mysqli->use_result())
$result->close();
while ($this->mysqli->next_result());
}
function connection_error()
{
die("<b>FATAL ERROR:</b> Could not connect to database on {$this->config->host} ($this->mysqli->connect_error)");
}
function query_error()
{
die("<b>QUERY ERROR:</b> ".$this->mysqli->error."<br />
Query was {$this->last_query}");
}
function fetch_single($result=0)
{
// http://stackoverflow.com/questions/1684993/what-does-mysqli-num-mean-and-do
// MYSQLI_NUM is a constant in PHP associated with a mysqli_result. If you're using mysqli to retrieve information from the database, MYSQLI_NUM can be used to specify the return format of the data. Specifically, when using the fetch_array function, MYSQLI_NUM specifies that the return array should use numeric keys for the array, instead of creating an associative array.
if(!$result) { $result=$this->result; }
$value = $result->fetch_array(MYSQLI_NUM);
return is_array($value) ? $value[0] : "";
}
function clean_input($in)
{
$in=stripslashes($in);
return str_replace(array("<",">",'"',"'","\n"), array("<",">",""","'","<br />"), $in);
}
function unhtmlize($text)
{
return str_replace("<br />","\n", $text);
}
function escape($text)
{
return $this->mysqli->real_escape_string($text);
}
function affected_rows($conn = NULL)
{
return $this->mysqli->affected_rows;
}
}