|
<?php
//------------------------------------------------------------------------------------------
// ※Database() 建構函式,資料庫初始參數
// ※Select() 查詢
// ※GetRows() 返回查詢的記錄總數
// ※Insert() 插入記錄
// ※Update() 更新
// ※Delete() 刪除
// ※Halt() 中斷並顯示錯誤資訊 111Cn.Net */
//------------------------------------------------------------------------------------------
define("DATABASETYPE", "1"); //定義資料庫類型:1為MySql;2為SQL Server;3為Oracle;4為Odbc
define("SERVER", "localhost"); //Host name or IP address of the database server
define("DATABASE", "dbName"); //要串連的資料庫名
define("USER", "tableName"); //用於串連資料庫的使用者名稱
define("PASSWORD", "paswd"); //用於串連資料庫的密碼
class Database {
var $dbLink; //串連控制代碼
var $result; //查詢控制代碼
var $insId; //Insert()成功返回AUTO_INCREMENT列的值
var $rows; //返回資料數組
var $numRows; //返回資料數目
var $dbHost, $dbUser, $userPassword, $database;
var $dbType = DATABASETYPE;
var $msgFlag = "yes"; //yes:show the Mysql message ; no: die by show "Halted."
function Database($dbHost = SERVER, $dbUser = USER, $userPassword = PASSWORD, $database = DATABASE) {
switch ($this->dbType) {
case 1:
$this->dbLink = @mysql_pconnect($dbHost, $dbUser, $userPassword); // or die("Can't Connect to Remote Host!");
@mysql_select_db($database, $this->dbLink); // or die ("Can't Connect to Remote Host!");
break;
case 2:
break;
}
return true;
}
/* SQL:Select() 返回為false無結果 */
function Select($table, $columns, $condition = 1) {
$sql = "select $columns from $table where $condition ";
$this->result = @mysql_query($sql, $this->dbLink);
unset($this->rows);
if ($this->result) {
$i = 0;
if (!($this->rows = array("$i" => @mysql_fetch_array($this->result))))
return false;
if (($this->numRows = @mysql_num_rows($this->result)) == 0)
return false;
while ($tempRows = @mysql_fetch_array($this->result)) {
array_push($this->rows, $tempRows);
}
} else {
$this->Halt($sql);
return false;
}
return true;
}
/* SQL:GetRows() 返回查詢的記錄總數 */
function GetRows($table, $condition = 1) {
$sql = "select count(1) as count from $table where $condition";
$this->result = @mysql_query($sql, $this->dbLink);
if ($this->result) {
$temp = @mysql_fetch_array($this->result);
$this->numRows = $temp[count];
} else {
$this->Halt($sql);
return false;
}
return $this->numRows;
}
/* SQL:Insert() */
function Insert($table, $columns, $values) {
$sql = "insert into $table ($columns) values ($values)";
$this->result = @mysql_query($sql, $this->dbLink);
if ($this->result)
$this->insId = @mysql_insert_id($this->dbLink);
else {
$this->Halt($sql);
return false;
}
return true;
}
/* SQL:Update() */
function Update($table, $setings, $condition) {
$sql = "update $table set $setings where $condition";
$this->result = @mysql_query($sql, $this->dbLink);
if ($this->result)
$this->numRows = @mysql_affected_rows($this->result);
else {
$this->Halt($sql);
return false;
}
return true;
}
/* SQL:Delete */
function Delete($table, $condition) {
$sql = "delete from $table where $condition";
$this->result = @mysql_query($sql, $this->dbLink);
if ($this->result)
$this->numRows = @mysql_affected_rows($this->result);
else {
$this->Halt($sql);
return false;
}
return true;
}
/* Halt():error message */
function Halt($msg) {
if ($this->msgFlag == "yes") {
printf("<b>Database Query Error:</b> %s<br>n", $msg);
printf("<b>MySql Error:</b> %s<br>n", mysql_error());
}else
echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=../include/error.htm'>"; //自定一個出錯提示檔案
return false;
}
}
switch ($db->dbType) {
case 1:
@mysql_close();
break;
case 2:
break;
}
$db = new Database();
?> |