01.
<?php
02.
require
(
"Settings_PHP.php"
);
03.
require
(
"ResultSet.php"
);
04.
class
Database {
05.
private
$connection
;
06.
static
$instances
= 0;
07.
private
$hostname
;
08.
private
$databasename
;
09.
private
$username
;
10.
private
$password
;
11.
private
$table
;
12.
private
$duptable
;
13.
function
__construct(
$db_id
=
"db0"
) {
14.
$settings
=
new
Settings_PHP();
15.
$settings
->load(
$_SERVER
[
"DOCUMENT_ROOT"
].
"/2012/config.php"
);
16.
$this
->hostname =
$settings
->get(
$db_id
.
".hostname"
);
17.
$this
->databasename =
$settings
->get(
$db_id
.
".database"
);
18.
$this
->username =
$settings
->get(
$db_id
.
".username"
);
19.
$this
->password =
$settings
->get(
$db_id
.
".password"
);
20.
21.
if
(Database::
$instances
== 0) {
22.
$this
->connection = mysql_connect(
$this
->hostname,
$this
->username,
$this
->password)
or
die
(mysql_error().
" Error no:"
.mysql_errno());
23.
mysql_query(
"SET NAMES TIS620"
);
24.
Database::
$instances
= 1;
25.
}
26.
else
{
27.
$msg
=
"Close the existing instance of the Database class."
;
28.
die
(
$msg
);
29.
}
30.
}
31.
function
__destruct() {
32.
$this
->close();
33.
}
34.
function
getConnection() {
35.
return
$this
->connection;
36.
}
37.
function
getResultSet(
$sql
) {
38.
$rs
=
new
ResultSet(
$sql
,
$this
->databasename,
$this
->connection);
39.
return
$rs
;
40.
}
41.
42.
function
execCommand(
$sql
) {
43.
mysql_db_query(
$this
->databasename,
$sql
)
or
die
(mysql_error().
" Error no:"
.mysql_errno());
44.
}
45.
function
getInsertID() {
46.
return
mysql_insert_id();
47.
}
48.
function
getEffectRow() {
49.
return
mysql_affected_rows();
50.
}
51.
function
export(
$table
,
$duptable
) {
52.
$this
->table =
$table
;
53.
$this
->duptable =
$duptable
;
54.
$header
=
$this
->create_header();
55.
return
(
$header
);
56.
}
57.
private
function
create_header() {
58.
$fields
= mysql_list_fields(
$this
->databasename,
$this
->table,
$this
->connection);
59.
$h
=
"CREATE TABLE `"
.
$this
->duptable .
"` ("
;
60.
$pkey
=
""
;
61.
$rs
= mysql_query(
"SHOW FIELDS FROM "
.
$this
->table);
62.
while
(
$row
= mysql_fetch_assoc(
$rs
)) {
63.
$name
=
$row
[
"Field"
];
64.
$type
=
$row
[
"Type"
];
65.
$null
=
$row
[
"Null"
];
66.
$key
=
$row
[
"Key"
];
67.
$default
=
$row
[
"Default"
];
68.
$extra
=
$row
[
"Extra"
];
69.
$h
.=
"`"
.
$name
.
"` "
.
$type
.
" "
.(
$null
==
"NO"
?
"NOT NULL"
:
"NULL"
).
" "
.
$extra
.
","
;
70.
if
(
$key
==
"PRI"
) {
71.
$pkey
.=
",PRIMARY KEY (`"
.
$name
.
"`)"
;
72.
}
73.
if
(
$key
==
"MUL"
) {
74.
$pkey
.=
",`"
.
$name
.
"` (`"
.
$name
.
"`)"
;
75.
}
76.
}
77.
$h
=
substr
(
$h
, 0,
strlen
(
$h
) - 1);
78.
$h
.=
$pkey
.
") TYPE=MyISAM DEFAULT CHARSET=tis620;"
;
79.
return
(
$h
);
80.
}
81.
function
close() {
82.
Database::
$instances
= 0;
83.
if
(isset(
$this
->connection)) {
84.
mysql_close(
$this
->connection);
85.
unset(
$this
->connection);
86.
}
87.
}
88.
}
89.
?>