/
db.function.php
171 lines (136 loc) · 3.6 KB
/
db.function.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
// db functions
function db( $host = null , $port = null , $user = null , $password = null , $db_name = null )
{
$db_key = MD5( $host .'-'. $port .'-'. $user .'-'. $password .'-'. $db_name );
if( !isset( $GLOBALS['LP_'.$db_key] ) )
{
@include_once( AROOT . 'config/db.config.sample.php' );
@include_once( AROOT . 'config/db.config.php' );
//include_once( CROOT . 'lib/db.function.php' );
$db_config = $GLOBALS['config']['db'];
if( $host == null ) $host = $db_config['db_host'];
if( $port == null ) $port = $db_config['db_port'];
if( $user == null ) $user = $db_config['db_user'];
if( $password == null ) $password = $db_config['db_password'];
if( $db_name == null ) $db_name = $db_config['db_name'];
if( !$GLOBALS['LP_'.$db_key] = mysql_connect( $host.':'.$port , $user , $password , true ) )
{
//
//echo 'can\'t connect to database';
$GLOBALS['LP_DB_CONNECT_ERROR'] = true;
$GLOBALS['LP_DB_CONNECT_ERROR_INFO'] = 'can\'t connect to database';
return false;
}
else
{
if( $db_name != '' )
{
if( !mysql_select_db( $db_name , $GLOBALS['LP_'.$db_key] ) )
{
//echo 'can\'t select database ' . $db_name ;
$GLOBALS['LP_DB_CONNECT_ERROR'] = true;
$GLOBALS['LP_DB_CONNECT_ERROR_INFO'] = 'can\'t select database ' . $db_name ;
return false;
}
}
}
if(@mysql_query( "SET NAMES 'UTF8'" , $GLOBALS['LP_'.$db_key] ))
{
$GLOBALS['LP_DB_CONNECT_ERROR'] = false;
$GLOBALS['LP_DB_CONNECT_ERROR_INFO'] = '' ;
}
}
return $GLOBALS['LP_'.$db_key];
}
function s( $str , $db = NULL )
{
if( $db == NULL ) $db = db();
return mysql_real_escape_string( $str , $db ) ;
}
// $sql = "SELECT * FROM `user` WHERE `name` = ?s AND `id` = ?i LIMIT 1 "
function prepare( $sql , $array )
{
foreach( $array as $k=>$v )
$array[$k] = s($v );
$reg = '/\?([is])/i';
$sql = preg_replace_callback( $reg , 'prepair_string' , $sql );
$count = count( $array );
for( $i = 0 ; $i < $count; $i++ )
{
$str[] = '$array[' .$i . ']';
}
$statement = '$sql = sprintf( $sql , ' . join( ',' , $str ) . ' );';
eval( $statement );
return $sql;
}
function prepair_string( $matches )
{
if( $matches[1] == 's' ) return "'%s'";
if( $matches[1] == 'i' ) return "'%d'";
}
function get_data( $sql , $db = NULL )
{
if( $db == NULL ) $db = db();
$GLOBALS['LP_LAST_SQL'] = $sql;
$data = Array();
$i = 0;
$result = mysql_query( $sql ,$db );
if( mysql_errno() != 0 )
echo mysql_error() .' ' . $sql;
while( $Array = mysql_fetch_array($result, MYSQL_ASSOC ) )
{
$data[$i++] = $Array;
}
if( mysql_errno() != 0 )
echo mysql_error() .' ' . $sql;
mysql_free_result($result);
if( count( $data ) > 0 )
return $data;
else
return false;
}
function get_line( $sql , $db = NULL )
{
$data = get_data( $sql , $db );
return @reset($data);
}
function get_var( $sql , $db = NULL )
{
$data = get_line( $sql , $db );
return $data[ @reset(@array_keys( $data )) ];
}
function last_id( $db = NULL )
{
if( $db == NULL ) $db = db();
return get_var( "SELECT LAST_INSERT_ID() " , $db );
}
function run_sql( $sql , $db = NULL )
{
if( $db == NULL ) $db = db();
$GLOBALS['LP_LAST_SQL'] = $sql;
return mysql_query( $sql , $db );
}
function db_errno( $db = NULL )
{
if( $db == NULL ) $db = db();
return mysql_errno( $db );
}
function db_error( $db = NULL )
{
if( $db == NULL ) $db = db();
return mysql_error( $db );
}
function last_error()
{
if( isset( $GLOBALS['LP_DB_LAST_ERROR'] ) )
return $GLOBALS['LP_DB_LAST_ERROR'];
}
function close_db( $db = NULL )
{
if( $db == NULL )
$db = $GLOBALS['LP_DB'];
unset( $GLOBALS['LP_DB'] );
mysql_close( $db );
}
?>