database.php
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
<?php
//****************************************************************************
// phpDatabase 2.1
//****************************************************************************
// Author: Maxim Poltarak <maxx at e dash taller dot net>
// Category: Databases
//****************************************************************************
// The lib is FREEWARE. This means you may use it anywhere you want, you may
// do anything with it. The Author mentioned above is NOT responsible for any
// consequences of using this library.
// If you don't agree with this, you MAY NOT use the lib!
//****************************************************************************
// All improvings, feature requests, bug reports, etc. are gladly accepted.
//****************************************************************************
// Note: For best viewing of the code Tab size 4 is recommended
//****************************************************************************
class CDatabase {
var $link;
var $db;
var $host, $user, $pass;
function db_connect($db, $host="localhost", $user="root", $pass="") {
$this->db = $db; $this->host = $host; $this->user = $user; $this->pass = $pass;
if($this->link = mysql_connect($host,$user,$pass))
return mysql_select_db($db, $this->link);
else return 0;
}
function query($sql) {
if(!$this->link) return 0;
return mysql_query($sql, $this->link);
}
function affected_rows() {
return mysql_affected_rows($this->link);
}
function num_rows($q) {
return mysql_num_rows($q);
}
function fetch_array($q, $result_type=MYSQL_ASSOC) {
return mysql_fetch_array($q, $result_type);
}
function fetch_object($q) {
return mysql_fetch_object($q);
}
function data_seek($q, $n) {
return mysql_data_seek($q, $n);
}
function free_result($q) {
return mysql_free_result($q);
}
function insert_id() {
return mysql_insert_id($this->link);
}
function error() {
return mysql_error($this->link);
}
function error_die($msg='') {
die(((empty($msg))?'':$msg.': ').$this->error());
}
function sql2var($sql) {
if((empty($sql)) || (!($query = $this->query($sql)))) return false;
if($this->num_rows($query) < 1) return false;
return $this->result2var($query);
}
function result2var($q) {
if(!($Data = $this->fetch_array($q))) return false;
$this->free_result($q);
foreach($Data as $k=>$v) $GLOBALS[$k] = $v;
return true;
}
function sql2array($sql, $keyField='') {
if((empty($sql)) || (!($query = $this->query($sql)))) return false;
if($this->num_rows($query) < 1) return false;
return $this->result2array($query, $keyField);
}
function result2array($q, $keyField='') {
$Result = array();
while($Data = $this->fetch_array($q))
if(empty($keyField)) $Result[] = $Data;
else $Result[$Data[$keyField]] = $Data;
$this->free_result($q);
return $Result;
}
function list_tables() {
return mysql_list_tables($this->db, $this->link);
}
function list_fields($table_name) {
return mysql_list_fields($this->db, $table_name, $this->link);
}
};
?>