-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.php
85 lines (75 loc) · 2.54 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
<?php
require_once 'conf.php';
class DB
{
var $dbh;
function DB()
{
$conf = new CONF();
$this->dbh = @mysql_connect($conf->host, $conf->user, $conf->pass) or die(mysql_error());
mysql_select_db($conf->db) or mysql_error($this->dbh);
if($conf->enc){
if (!function_exists('mysql_set_charset')) mysql_query("set names '{$conf->enc}'", $this->dbh);
else mysql_set_charset($conf->enc, $this->dbh);
}
}
function _query($query){
$resp = mysql_query($query, $this->dbh);
if(!$resp) throw new Exception(mysql_error()."\n\nquery: ".$query);
return $resp;
}
function _conds($campos, $unir = 'AND'){
if(empty($campos)) return '1>0';
if(!is_array($campos)) return $campos;
$conds = array();
foreach($campos as $campo => $valor){
if($campo=='AND' || $campo=='OR') $conds[] = $this->_conds($valor, $campo); //arbol de condiciones
else if(is_array($valor)){ //campo in valores
$c = '';
$kn = array_search(null, $valor, true);
if($kn !== false){
$c = $campo.' IS NULL OR ';
unset($valor[$kn]);
}
foreach($valor as $k=>$v) $valor[$k] = "'".addslashes($v)."'";
$conds[] = $c.$campo.' IN ('.implode(', ', $valor).')';
}
else if(is_numeric($campo)) $conds[] = $valor; //condicion random
else $conds[] = $campo.($valor===null ? " IS NULL" : " = '".addslashes($valor)."'");
}
return '('.implode(' '.$unir.' ', $conds).')';
}
function select($cols, $tabla, $conds='', $extra=''){
$q = $this->_query("SELECT $cols FROM $tabla WHERE ".$this->_conds($conds).' '.$extra);
$res = array();
while(true){
$fila = mysql_fetch_array($q, MYSQL_ASSOC);
if(!$fila) break;
foreach($fila as &$c) if(is_numeric($c)) $c*=1;
$res[] = $fila;
}
return $res;
}
function campo($col, $tabla, $conds_campos, $extra=''){
$resp = $this->select($col, $tabla, $conds_campos, $extra.' LIMIT 0,1');
if(empty($resp)) return false;
return $resp[0][$col];
}
function insert($tabla, $campos){
$datos = array();
foreach($campos as $campo => $valor)
$datos[] = $campo." = ".($valor===null ? "NULL" : "'".addslashes($valor)."'");
$resp = $this->_query("INSERT INTO $tabla SET ".implode(', ', $datos));
return mysql_insert_id($this->dbh);
}
function update($tabla, $campos, $conds){
$datos = array();
foreach($campos as $campo => $valor)
$datos[] = $campo." = ".($valor===null ? "NULL" : "'".addslashes($valor)."'");
return $this->_query("UPDATE $tabla SET ".implode(', ', $datos)." WHERE ".$this->_conds($conds));
}
function hash($pass){
return sha1('23aeroiyna4omw38jrlbnltif23' . $pass);
}
}
?>