<
?
PHP
/*
File Name: database. php
Role: Database
Author: mr. bool
Created at: 2007.10.22
Modification time: 2007.10.23
*/
Require_once
(
"Config. Inc. php"
)
;
// System Configuration File
/*
$ Dbhost = "localhost"; // host name
$ Dbuser = "root"; // database username
$ Dbpwd = ""; // Password
$ Dbname = "test"; // Database Name
*/
// Define the Database Class
Class
Database
{
// Define attributes
VaR
$
Mconnid
;
// Connection ID
VaR
$
Msqlstring
;
// SQL statement to be executed
VaR
$
Mresultarray
;
// An array of results returned by executing the SELECT statement
// _ Construct (), constructor to establish a database connection
Function
_ Construct (
$
Phost
,
$
Puser
,
$
Ppwd
,
$
Pdbname
)
{
$
This
-
>
Mconnid =
Mysql_connect
(
$
Phost
,
$
Puser
,
$
Ppwd
)
;
// Establish a connection
Mysql_select_db
(
$
Pdbname
,
$
This
-
>
Mconnid)
;
// Select a database
Mysql_query
(
"Set names 'gbk '"
)
;
// Set the database encoding to GBK
}
// _ Destruct: destructor, disconnected
Function
_ Destruct (
)
{
Mysql_close
(
$
This
-
>
Mconnid)
;
// There is still a problem here ......
}
// Execute the SQL statement
Function
Executesql (
)
{
Mysql_query
(
$
This
-
>
Msqlstring)
;
}
// Query data. The returned value is an array of objects. Each element in the array is an object composed of a row of records.
Function
Query (
)
{
$
I
=
0;
$
Query_result
=
Mysql_query
(
$
This
-
>
Msqlstring,
$
This
-
>
Mconnid)
;
While
(
$
Row
=
Mysql_fetch_object
(
$
Query_result
)
)
{
$
This
-
>
Mresultarray [
$
I
+
+
]
=
$
Row
;
}
}
}
// Class database
// Use the following for testing:
$
DB
=
New
Database (
$
Dbhost
,
$
Dbuser
,
$
Dbpwd
,
$
Dbname
)
;
$
DB
-
>
Msqlstring =
"Update student set phone = '000000' where id = '000000 '"
;
$
DB
-
>
Executesql (
)
;
$
DB
-
>
Msqlstring =
"Select * from student where id = '000000 '"
;
$
DB
-
>
Query (
)
;
Print_r
(
$
DB
-
>
Mresultarray)
;
// Output the test result
?
>