-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmysql.class.php
More file actions
173 lines (148 loc) · 3.74 KB
/
mysql.class.php
File metadata and controls
173 lines (148 loc) · 3.74 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* phpSQL : php_mysql
*
* WARNING: Use of this class is NOT recommended!! The php_mysql extension does not support prepared statements, among other things.
* This class gets around that by simulating them, but your script will still be vulnerable to SQL injection attacks!
*
* This class is intended for isolated legacy support cases. It is strongly recommended that you use php_mysqli instead!
*/
require_once( "config_sql.class.php" );
class SQL extends Config_SQL
{
public function __construct()
{
$args = func_get_args();
foreach ( $args as $argarr )
{
foreach ( $argarr as $key => $value )
{
$this->$key = $value;
}
}
/* Load the override settings, if specified. --Kris */
if ( isset( $this->sql_obj ) && isset( $this->sql_obj[$this->sql_obj] ) )
{
foreach ( $this->sql_obj[$this->sql_obj] as $var => $val )
{
$this->$var = $val;
}
}
$this->link = mysql_connect( $this->sql_host, $this->sql_user, $this->sql_pass ) or die( "Unable to connect to MySQL : " . mysql_error() );
mysql_select_db( $this->sql_db, $this->link ) or die( "Unable to connect to database : " . mysql_error() );
}
function query( $query, $params = array(), $returntype = 1 )
{
/* Prepared statements aren't supported, so let's just cheat and convert them. --Kris */
if ( !empty( $params ) )
{
if ( substr_count( $query, "?" ) != count( $params ) )
{
die( "(php_mysql Conversion Error): Number of variables doesn't match number of parameters in prepared statement." );
}
$parseloop = 0;
$newquery = NULL;
foreach ( $params as $pkey => $val )
{
for ( $parseloop = $parseloop; $parseloop < strlen( $query ); $parseloop ++ )
{
if ( strcmp( $query{$parseloop}, "?" ) == 0 )
{
if ( is_numeric( $val ) )
{
$newquery .= $val;
}
else
{
$newquery .= "'" . $val . "'";
}
$parseloop++;
break;
}
else
{
$newquery .= $query{$parseloop};
}
}
}
for ( $parseloop = $parseloop; $parseloop < strlen( $query ); $parseloop++ )
{
$newquery .= $query{$parseloop};
}
$query = $newquery;
}
$result = mysql_query( $query ) or die( "Query '$query' failed : " . mysql_error() );
switch ( $returntype )
{
default:
case 0:
$returnvar = NULL;
break;
case 1:
$returnvar = self::fetch( $result );
break;
case 2:
$returnvar = mysql_affected_rows();
break;
case 3:
$returnvar = mysql_num_rows( $result );
break;
case 4:
$returnvar = $result;
break;
}
return $returnvar;
}
function fetch( $result )
{
$resdata = array();
while ( $line = mysql_fetch_array( $result, MYSQL_ASSOC ) )
{
$tmp = array();
$sqloop = 0;
foreach ( $line as $col_value )
{
$fieldname = mysql_field_name( $result, $sqloop );
if ( strcmp( intval( $col_value ), $col_value ) == 0 )
{
/* To mimic the mysqli behavior as closely as possible. --Kris */
$tmp[$fieldname] = (int) $col_value;
}
else
{
$tmp[$fieldname] = $col_value;
}
$sqloop++;
}
$resdata[] = $tmp;
}
return $resdata;
}
function close()
{
return mysql_close( $this->link );
}
function build_where_clause( $columns = array(), $values = array(), $and = TRUE )
{
if ( ( empty( $columns ) && empty( $values ) )
|| count( $columns ) != count( $values ) )
{
return FALSE;
}
$andor = ( $and == TRUE ? "AND" : "OR" );
$where = NULL;
foreach ( $columns as $ckey => $col )
{
if ( $where != NULL )
{
$where .= " $andor ";
}
$where .= "$col = ?";
}
return array( 0 => $where, 1 => $values );
}
function addescape( $string )
{
return addslashes( $string );
}
}