PHP 數據庫鏈接類
1 <?php
2 class Connection
3 {
4 var $Server;
5 var $UserName;
6 var $Password;
7 var $Database;
8 private $_conn;
9
10 public function __construct($server,$username,$password,$database)
11 {
12 $this->Server = $server;
13 $this->UserName = $username;
14 $this->Password = $password;
15 $this->Database = $database;
16 }
17
18 public function Open()
19 {
20 try
21 {
22 $this->_conn = mysql_connect($this->Server,$this->UserName,$this->Password);
23 mysql_select_db($this->Database);
24 }
25 catch (Exception $ex)
26 {
27 throw new Exception($ex->getMessage());
28 }
29 }
30
31 public function Close()
32 {
33 mysql_close($this->_conn);
34 }
35
36 public function Execute($sqlString)
37 {
38 try
39 {
40 mysql_query($sqlString, $this->_conn);
41 }
42 catch (Exception $ex)
43 {
44 throw new Exception($ex->getMessage());
45 }
46 }
47
48 public function Query($sqlString)
49 {
50 $resource = mysql_query($sqlString, $this->_conn);
51 if ($resource)
52 {
53 if (is_resource($resource))
54 {
55 $i = 1;
56 $rows = array();
57 while ($result = mysql_fetch_assoc($resource))
58 {
59 $rows[$i] = $result;
60 $i++;
61 }
62 $table = new DataTable($rows);
63 return $table;
64 }
65 }
66 else
67 {
68 $errorString = 'Error: ' . mysql_error($this->_conn);
69 $errorString .= ' (Error No: ' . mysql_errno($this->_conn) . ')';
70 throw new Exception($errorString);
71 }
72 }
73
74 public function GetLastId()
75 {
76 return mysql_insert_id($this->connection);
77 }
78 }
79 ?>
2 class Connection
3 {
4 var $Server;
5 var $UserName;
6 var $Password;
7 var $Database;
8 private $_conn;
9
10 public function __construct($server,$username,$password,$database)
11 {
12 $this->Server = $server;
13 $this->UserName = $username;
14 $this->Password = $password;
15 $this->Database = $database;
16 }
17
18 public function Open()
19 {
20 try
21 {
22 $this->_conn = mysql_connect($this->Server,$this->UserName,$this->Password);
23 mysql_select_db($this->Database);
24 }
25 catch (Exception $ex)
26 {
27 throw new Exception($ex->getMessage());
28 }
29 }
30
31 public function Close()
32 {
33 mysql_close($this->_conn);
34 }
35
36 public function Execute($sqlString)
37 {
38 try
39 {
40 mysql_query($sqlString, $this->_conn);
41 }
42 catch (Exception $ex)
43 {
44 throw new Exception($ex->getMessage());
45 }
46 }
47
48 public function Query($sqlString)
49 {
50 $resource = mysql_query($sqlString, $this->_conn);
51 if ($resource)
52 {
53 if (is_resource($resource))
54 {
55 $i = 1;
56 $rows = array();
57 while ($result = mysql_fetch_assoc($resource))
58 {
59 $rows[$i] = $result;
60 $i++;
61 }
62 $table = new DataTable($rows);
63 return $table;
64 }
65 }
66 else
67 {
68 $errorString = 'Error: ' . mysql_error($this->_conn);
69 $errorString .= ' (Error No: ' . mysql_errno($this->_conn) . ')';
70 throw new Exception($errorString);
71 }
72 }
73
74 public function GetLastId()
75 {
76 return mysql_insert_id($this->connection);
77 }
78 }
79 ?>