php 資料庫mysql查詢與串連類

來源:互聯網
上載者:User

<?php


class mysql
{
 var $host     = "";   //mysql主機名稱
 var $user     = "";   //mysql使用者名稱
 var $pwd      = "";   //mysql密碼
 var $dbName   = "";   //mysql資料庫名稱
 var $linkID   = 0;   //用來儲存串連ID
 var $queryID  = 0;   //用來儲存查詢ID
 var $fetchMode= MYSQL_ASSOC;//取記錄時的模式
 var $queryTimes = 0;  //儲存查詢的次數
 var $errno    = 0;   //mysql出錯代號
 var $error    = "";   //mysql出錯資訊
 var $record   = array(); //一條記錄數組

 //======================================
 // 函數: mysql()
 // 功能: 建構函式
 // 參數: 參數類的變數定義
 // 說明: 建構函式將自動連接資料庫
 //       如果想手動串連去掉串連函數
 //======================================
 function mysql($host,$user,$pwd,$dbName)
 { if(empty($host) || empty($user) || empty($dbName))
   $this->halt("資料庫主機地址,使用者名稱或資料庫名稱不完全,請檢查!");
  $this->host    = $host;
  $this->user    = $user;
  $this->pwd     = $pwd;
  $this->dbName  = $dbName;
  $this->connect();//設定為自動連接
 }
 //======================================
 // 函數: connect($host,$user,$pwd,$dbName)
 // 功能: 串連資料庫
 // 參數: $host 主機名稱, $user 使用者名稱
 // 參數: $pwd 密碼, $dbName 資料庫名稱
 // 返回: 0:失敗
 // 說明: 預設使用類中變數的初始值
 //======================================
 function connect($host = "", $user = "", $pwd = "", $dbName = "")
 {
  if ("" == $host)
   $host = $this->host;
  if ("" == $user)
   $user = $this->user;
  if ("" == $pwd)
   $pwd = $this->pwd;
  if ("" == $dbName)
   $dbName = $this->dbName;
  //now connect to the database
  $this->linkID = mysql_pconnect($host, $user, $pwd);
  if (!$this->linkID)
  {
   $this->halt();
   return 0;
  }
  if (!mysql_select_db($dbName, $this->linkID))
  {
   $this->halt();
   return 0;
  }
  return $this->linkID;   
 }
 //======================================
 // 函數: query($sql)
 // 功能: 資料查詢
 // 參數: $sql 要查詢的SQL語句
 // 返回: 0:失敗
 //======================================
 function query($sql)
 {
  $this->queryTimes++;
  $this->queryID = mysql_query($sql, $this->linkID);
  if (!$this->queryID)
  { 
   $this->halt();
   return 0;
  }
  return $this->queryID;
 }
 //======================================
 // 函數: setFetchMode($mode)
 // 功能: 設定取得記錄的模式
 // 參數: $mode 模式 MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH
 // 返回: 0:失敗
 //======================================
 function setFetchMode($mode)
 {
  if ($mode == MYSQL_ASSOC || $mode == MYSQL_NUM || $mode == MYSQL_BOTH)
  {
   $this->fetchMode = $mode;
   return 1;
  }
  else
  {
   $this->halt("錯誤的模式.");
   return 0;
  }
  
 }
 //======================================
 // 函數: fetchRow()
 // 功能: 從屬記錄集中取出一條記錄
 // 返回: 0: 出錯 record: 一條記錄
 //======================================
 function fetchRow()
 {
  $this->record = mysql_fetch_array($this->queryID,$this->fetchMode);
  return $this->record;
 }
 //======================================
 // 函數: fetchAll()
 // 功能: 從屬記錄集中取出所有記錄
 // 返回: 記錄集數組
 //======================================
 function fetchAll()
 {
  $arr = array();
  while($this->record = mysql_fetch_array($this->queryID,$this->fetchMode))
  {
   $arr[] = $this->record;
  }
  mysql_free_result($this->queryID);
  return $arr;
 }
 //======================================
 // 函數: getValue()
 // 功能: 返回記錄中指定欄位的資料
 // 參數: $field 欄位名或欄位索引
 // 返回: 指定欄位的值
 //======================================
 function getValue($field)
 {
  return $this->record[$field];
 }
 //======================================
 // 函數: affectedRows()
 // 功能: 返回影響的記錄數
 //======================================
 function affectedRows()
 {
  return mysql_affected_rows($this->linkID);
 }
 //======================================
 // 函數: recordCount()
 // 功能: 返回查詢記錄的總數
 // 參數: 無
 // 返回: 記錄總數
 //======================================
 function recordCount()
 {
  return mysql_num_rows($this->queryID);
 }
 //======================================
 // 函數: getQueryTimes()
 // 功能: 返回查詢的次數
 // 參數: 無
 // 返回: 查詢的次數
 //======================================
 function getQueryTimes()
 {
  return $this->queryTimes;
 }
 //======================================
 // 函數: getVersion()
 // 功能: 返回mysql的版本
 // 參數: 無
 //======================================
 function getVersion()
 {
  $this->query("select version() as ver");
  $this->fetchRow();
  return $this->getValue("ver");
 }
 //======================================
 // 函數: getDBSize($dbName, $tblPrefix=null)
 // 功能: 返回資料庫佔用空間大小
 // 參數: $dbName 資料庫名
 // 參數: $tblPrefix 表的首碼,可選
 //======================================
 function getDBSize($dbName, $tblPrefix=null)
 {
  $sql = "SHOW TABLE STATUS FROM " . $dbName;
  if($tblPrefix != null) {
   $sql .= " LIKE '$tblPrefix%'";
  }
  $this->query($sql);
  $size = 0;
  while($this->fetchRow())
   $size += $this->getValue("Data_length") + $this->getValue("Index_length");
  return $size;
 }
 //======================================
 // 函數: insertID()
 // 功能: 返回最後一次插入的自增ID
 // 參數: 無
 //======================================
 function insertID() {
  return mysql_insert_id();
 }
 //======================================
 // 函數: halt($err_msg)
 // 功能: 處理所有出錯資訊
 // 參數: $err_msg 自訂的出錯資訊
 //=====================================
 function halt($err_msg="")
 {
  if ("" == $err_msg)
  {
   $this->errno = mysql_errno();
   $this->error = mysql_error();
   echo "<b>mysql error:<b><br>";
   echo $this->errno.":".$this->error."<br>";
   exit;
  }
  else
  {
   echo "<b>mysql error:<b><br>";
   echo $err_msg."<br>";
   exit;
  }
 }
}
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.