<?php
/**
* 數據庫處理類,只能處理mysql類型的數據庫
* @author lian0707
* @package database
* @version 1.0
*/
class mysql{
var $_queryNum = 0;
/**
* 構造函數
*
* @param string $sDBHost
* @param string $sDBUser
* @param string $sDBPassWord
* @param string $sDBName
*/
function __construct($sDBHost, $sDBName, $sDBUser, $sDBPassWord, $bDebug = 0)
{
$this->__DBHost = $sDBHost;
$this->__DBName = $sDBName;
$this->__DBUser = $sDBUser;
$this->__DBPassWord = $sDBPassWord;
$this->__debug = $bDebug;
}
/**
* 連接數據庫
*
* @return true on success, false on failure
*/
function connect(){
$this->__moDB = mysql_connect($this->__DBHost, $this->__DBUser, $this->__DBPassWord);
mysql_select_db($this->__DBName,$this->__moDB);
return true;
}
/**
* 執行query操作
*
* @param string $sSQL
* @return source
*/
function query($sSQL){
$this->__queryNum++;
if($this->__debug)
{
$this->showDebug($sSQL);
}
//判斷裡面有沒有insert,delete,update等操作,如果有不允許在這裡操作
$this->__moQuery = mysql_query($sSQL, $this->__moDB);
return $this->__moQuery;
}
/**
* 得到記錄數組
*
* @param unknown_type $sSQL
* @param unknown
*/
function getRecordSet($sSQL, $iCount = NULL){
//這裡檢測注入危害,sql中不能出現delete和update
$aRes = $this->query($sSQL);
$aList = array();
while ($rr = mysql_fetch_array($aRes, MYSQL_ASSOC))
{
$aList[] = $rr;
}
if($iCount <> 1)
return $aList;
else
return $aList[0];
}
/**
* 插入數據到指定的數據表
*
* @param string $sTableName
* @param array $aField
*/
function insert($sTableName, $aField){
$sSQL = "INSERT INTO `{$sTableName}` ";
$sField = "(";
$sValue = "(";
foreach ($aField as $rk => $rv)
{
$sField .= $rk.",";
$sValue .= "'".$rv."',";
}
$sField = substr($sField, 0, -1).")";
$sValue = substr($sValue, 0, -1).")";
$sSQL .= $sField." VALUES ".$sValue;
$this->__queryNum++;
if($this->__debug)
$this->showDebug($sSQL);
mysql_query($sSQL,$this->__moDB);
$iInsertID = mysql_insert_id($this->__moDB);
return $iInsertID;
}
/**
* 更新操作
*
* @param string $sTableName
* @param array $aField
* @param string $sWhere
*/
function update($sTableName, $aField, $sWhere){
$sSQL = "UPDATE {$sTableName} SET ";
$sField = "";
foreach ($aField as $rk => $rv){
$sField .= $rk.="='".$rv."',";
}
$sField = substr($sField, 0, -1);
$sSQL .= $sField." WHERE {$sWhere}";
$this->__queryNum++;
if($this->__debug)
$this->showDebug($sSQL);
return mysql_query($sSQL,$this->__moDB);
}
/**
* 刪除
*
* @param string $sTableName
* @param string $sWhere
*/
function delete($sTableName, $sWhere){
$sSQL = "DELETE FROM `{$sTableName}` WHERE {$sWhere}";
$this->__queryNum++;
if($this->__debug)
$this->showDebug($sSQL);
return mysql_query($sSQL,$this->__moDB);
}
/**
* 取得結果集中行的數目
*
* @param unknown_type $oQuery
* @return unknown
*/
function num_rows($oQuery){
$query = mysql_num_rows($oQuery);
return $query;
}
/**
* 取得結果集中字段的數目
*
* @param unknown_type $oQuery
* @return unknown
*/
function num_fields($oQuery){
$query = mysql_num_fields($oQuery);
return $query;
}
/**
* 取得結果數據
*/
function result($oQuery, $iRow, $sField) {
return mysql_result($oQuery, $iRow, $sField);
}
/**
* 得到查詢後的數量
*
* @param string $sSQL
* @return unknown
*/
function getRowsNum($sSQL){
$oQuery = mysql_query($sSQL);
if(strpos(strtolower($sSQL),"count("))
{
$aRes = mysql_fetch_array($oQuery);
return $aRes[0];
}
else
{
$iNums = (mysql_num_rows($oQuery)) ? mysql_num_rows($oQuery) : 0;
return $iNums;
}
}
/**
* 顯示出現的錯誤
*
* @param unknown_type $sSQL
*/
function showDebug($sSQL){
echo "<br />{$this->__queryNum}. SQL:$sSQL";
}
/**
* 判斷數據是否已經存在
*
* @param string $sTable -查詢的表
* @param string $sWhere -查詢的條件
* @param int $iType -類型: $iType=0表示添加;$iType=1表示修改
* @return unknown
*/
function isExists($sTable, $sWhere, $iType=0){
global $db;
$sSQL = "SELECT id FROM " . $sTable . " WHERE " . $sWhere;
if(count($db->getRecordSet($sSQL, 1)) > $iType)
return true;
else
return false;
}
}
?>