-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadb.php
More file actions
executable file
·137 lines (110 loc) · 3.08 KB
/
Copy pathadb.php
File metadata and controls
executable file
·137 lines (110 loc) · 3.08 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
<?php
/**
* author:
* date:
* description: A root class for all manage classes. This class communicates with DB
*/
define("DB_HOST", 'localhost');
define("DB_NAME", 'news_on_time');
define("DB_PORT", 3306);
define("DB_USER","root");
define("DB_PWORD","");
define("LOG_LEVEL_SEC",0);
define("LOG_LEVEL_DB_FAIL",0);
function log_msg($level, $er_code, $msg, $mysql_msg){
return 0;
}
class adb {
/**error description*/
var $str_error;
/*error code*/
var $error;
/*db connection link*/
var $link;
/* Every error log has a 4 digit code. The first two digits(prefix) tells you which class logged the error*/
var $er_code_prefix;
/* query result resource*/
var $result;
function adb() {
$this->er_code_prefix=1000;
$this->link=false;
$this->result = false;
}
/**
* logs error into database using functions defined in log.php
*/
function log_error($level, $code, $msg, $mysql_msg = "NONE") {
$er_code = $this->er_code_prefix + $code;
//call to a predefined function
$log_id = log_msg($level, $er_code, $msg, $mysql_msg);
//if log id is false return 0;
if (!$log_id) {
return 0;
}
//display this code to user
$this->error="$er_code-$log_id";
return $log_id;
}
/**
* creates connection to database
*/
function connect() {
if($this->link)
{
return true;
}
//try to connect to db
$this->link = mysql_connect(DB_HOST , DB_USER, DB_PWORD);
if (!$this->link) {
//if connection fail log error and set $str_error
echo "not connected"; //debug line
$this->log_error(LOG_LEVEL_DB_FAIL,1, "connection failed in db:connect()", mysql_error());
return false;
}
if (!mysql_select_db(DB_NAME)) {
$log_id = $this->log_error(LOG_LEVEL_DB_FAIL,2, "select db failed in db:connect()", mysql_error($this->link));
return false;
}
return true;
}
/**
*returns a row from a data source
*/
function fetch() {
return mysql_fetch_assoc($this->result);
}
/**
* connect to db and run a query
*/
function query($str_sql) {
if (!$this->connect()) {
return false;
}
$this->result = mysql_query($str_sql);
if (!$this->result) {
$this->log_error(LOG_LEVEL_DB_FAIL, 4, "query failed", mysql_error($this->link));
return false;
}
return true;
}
/**
* returns number of rows in current dataset
*/
function get_num_rows() {
return mysql_num_rows($this->result);
}
/**
*returns last auto generated id
*/
function get_insert_id() {
return mysql_insert_id($this->link);
}
}
// $obj = new adb();
// if($obj->connect()){
// echo "connected";
// }
// else{
// echo"not connected";
// }
?>