-
Notifications
You must be signed in to change notification settings - Fork 7
/
db.php
96 lines (89 loc) · 2.81 KB
/
db.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
<?php
// Various DB functions
// Connect to DB
function db_connect() {
global $db_host,$db_login,$db_password,$db_base;
$res=mysql_pconnect($db_host,$db_login,$db_password);
mysql_select_db($db_base);
// db_query("SET NAMES 'utf8'");
}
// Query
function db_query($query) {
$result=mysql_query($query);
if($result===FALSE) {
$query_escaped=db_escape($query);
mysql_query("INSERT INTO `log` (`message`) VALUES ('$query_escaped')");
die("Query error: $query");
}
return $result;
}
// Query and return results array
function db_query_to_array($query) {
$result_array=array();
$result=db_query($query);
if(mysql_num_rows($result)) {
while($row=mysql_fetch_assoc($result)) {
$result_array[]=$row;
}
}
return $result_array;
}
// Escape string
function db_escape($string) {
return mysql_real_escape_string($string);
}
// Escape string
function db_escape_ascii($string) {
$result="";
for($i=0;$i!=strlen($string);$i++) {
if(ord($string[$i])>=32 && ord($string[$i])<=127) $result.=$string[$i];
}
return mysql_real_escape_string($result);
}
// Query and return value from first row first column
function db_query_to_variable($query) {
$result=db_query($query);
if(mysql_num_rows($result)) {
$row=mysql_fetch_array($result);
$res=$row[0];
} else {
$res="";
}
return $res;
}
// For php7
if(!function_exists("mysql_pconnect")) {
function mysql_pconnect($host,$login,$password) {
global $mysqli_res;
$mysqli_res=mysqli_connect($host,$login,$password);
}
function mysql_select_db($db) {
global $mysqli_res;
return mysqli_select_db($mysqli_res,$db);
}
function mysql_query($query) {
global $mysqli_res;
return mysqli_query($mysqli_res,$query);
}
function mysql_fetch_assoc($resource) {
global $mysqli_res;
return mysqli_fetch_assoc($resource);
}
function mysql_fetch_array($resource) {
global $mysqli_res;
return mysqli_fetch_array($resource);
}
function mysql_num_rows($resource) {
global $mysqli_res;
return mysqli_num_rows($resource);
}
function mysql_real_escape_string($str) {
global $mysqli_res;
return mysqli_real_escape_string($mysqli_res,$str);
}
function mysql_insert_id() {
global $mysqli_res;
return mysqli_insert_id($mysqli_res);
}
}
?>