001.
<?
002.
003.
004.
005.
006.
007.
008.
if
(
eregi
(
"class.mysql.php"
,
$_SERVER
[
'PHP_SELF'
])) {
009.
Header(
"Location: ../index.php"
);
010.
die
();
011.
}
012.
013.
class
DB{
014.
015.
var
$host
= DB_HOST ;
016.
var
$database
;
017.
var
$connect_db
;
018.
var
$selectdb
;
019.
var
$db
;
020.
var
$sql
;
021.
var
$table
;
022.
var
$where
;
023.
024.
025.
026.
function
connectdb(
$db_name
=
"database"
,
$user
=
"username"
,
$pwd
=
"password"
){
027.
$this
->database =
$db_name
;
028.
$this
->username =
$user
;
029.
$this
->password =
$pwd
;
030.
$this
->connect_db = mysql_connect (
$this
->host,
$this
->username,
$this
->password )
or
sql_error (
"database-connect"
, mysql_error() );
031.
032.
$this
->db = mysql_select_db (
$this
->database,
$this
->connect_db)
or
sql_error (
"database-select"
, mysql_error() );
033.
mysql_query(
"SET NAMES TIS620"
);
034.
mysql_query(
"SET character_set_results=tis620"
);
035.
return
true;
036.
}
037.
038.
039.
function
closedb( ){
040.
mysql_close (
$this
->connect_db )
or
sql_error (
"database-close"
, mysql_error() );
041.
}
042.
043.
044.
045.
function
add_db(
$table
=
"table"
,
$data
=
"data"
){
046.
$key
=
array_keys
(
$data
);
047.
$value
=
array_values
(
$data
);
048.
$sumdata
=
count
(
$key
);
049.
for
(
$i
=0;
$i
<
$sumdata
;
$i
++)
050.
{
051.
if
(
empty
(
$add
)){
052.
$add
=
"("
;
053.
}
else
{
054.
$add
=
$add
.
","
;
055.
}
056.
if
(
empty
(
$val
)){
057.
$val
=
"("
;
058.
}
else
{
059.
$val
=
$val
.
","
;
060.
}
061.
$add
=
$add
.
$key
[
$i
];
062.
$val
=
$val
.
"'"
.
$value
[
$i
].
"'"
;
063.
}
064.
$add
=
$add
.
")"
;
065.
$val
=
$val
.
")"
;
066.
$sql
=
"INSERT INTO "
.
$table
.
" "
.
$add
.
" VALUES "
.
$val
;
067.
if
(mysql_query(
$sql
)){
068.
return
true;
069.
}
else
{
070.
$this
->_error();
071.
return
false;
072.
}
073.
}
074.
075.
076.
077.
function
update_db(
$table
=
"table"
,
$data
=
"data"
,
$where
=
"where"
){
078.
$key
=
array_keys
(
$data
);
079.
$value
=
array_values
(
$data
);
080.
$sumdata
=
count
(
$key
);
081.
$set
=
""
;
082.
for
(
$i
=0;
$i
<
$sumdata
;
$i
++)
083.
{
084.
if
(!
empty
(
$set
)){
085.
$set
=
$set
.
","
;
086.
}
087.
$set
=
$set
.
$key
[
$i
].
"='"
.
$value
[
$i
].
"'"
;
088.
}
089.
$sql
=
"UPDATE "
.
$table
.
" SET "
.
$set
.
" WHERE "
.
$where
;
090.
if
(mysql_query(
$sql
)){
091.
return
true;
092.
}
else
{
093.
$this
->_error();
094.
return
false;
095.
}
096.
}
097.
098.
099.
100.
function
update(
$table
=
"table"
,
$set
=
"set"
,
$where
=
"where"
){
101.
$sql
=
"UPDATE "
.
$table
.
" SET "
.
$set
.
" WHERE "
.
$where
;
102.
if
(mysql_query(
$sql
)){
103.
return
true;
104.
}
else
{
105.
$this
->_error();
106.
return
false;
107.
}
108.
}
109.
110.
111.
112.
function
del(
$table
=
"table"
,
$where
=
"where"
){
113.
$sql
=
"DELETE FROM "
.
$table
.
" WHERE "
.
$where
;
114.
if
(mysql_query(
$sql
)){
115.
return
true;
116.
}
else
{
117.
$this
->_error();
118.
return
false;
119.
}
120.
}
121.
122.
123.
124.
function
num_rows(
$table
=
"table"
,
$field
=
"field"
,
$where
=
"where"
) {
125.
if
(
$where
==
""
) {
126.
$where
=
""
;
127.
}
else
{
128.
$where
=
" WHERE "
.
$where
;
129.
}
130.
$sql
=
"SELECT "
.
$field
.
" FROM "
.
$table
.
$where
;
131.
if
(
$res
= mysql_query(
$sql
)){
132.
return
mysql_num_rows(
$res
);
133.
}
else
{
134.
$this
->_error();
135.
return
false;
136.
}
137.
}
138.
139.
140.
141.
function
select_query(
$sql
=
"sql"
){
142.
if
(
$res
= mysql_query(
$sql
)){
143.
return
$res
;
144.
}
else
{
145.
$this
->_error();
146.
return
false;
147.
}
148.
}
149.
150.
151.
152.
153.
function
rows(
$sql
=
"sql"
){
154.
if
(
$res
= mysql_num_rows(
$sql
)){
155.
return
$res
;
156.
}
else
{
157.
$this
->_error();
158.
return
false;
159.
}
160.
}
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
function
fetch(
$sql
=
"sql"
){
171.
if
(
$res
= mysql_fetch_assoc(
$sql
)){
172.
return
$res
;
173.
}
else
{
174.
$this
->_error();
175.
return
false;
176.
}
177.
}
178.
179.
180.
function
_error(){
181.
$this
->error[]=mysql_errno();
182.
}
183.
184.
}
185.
?>