首頁  >  文章  >  後端開發  >  lineargradientbrush 加強版phplib的DB類

lineargradientbrush 加強版phplib的DB類

WBOY
WBOY原創
2016-07-29 08:37:46869瀏覽

複製程式碼 程式碼如下:


/************************************* ************************************************** *************************
為了方便自己的開發,又不想使用ADODB、PEAR::DB這樣的龐然大物,
就用在PHPLib DB類別的基礎、參考PEAR::DB類,封裝的DB類,簡單好使,非常方便。
MySQL有效
[ 连接数据库 ]
//包含数据库处理类文件
include_once("database.inc.php");
//本地数据库配置
define("DB_HOST",    "localhost"); //数据库服务器
define("DB_USER_NAME",   "root");  //数据库用户名
define("DB_USER_PASS",   "");   //密码
define("DB_DATABASE",   "test");  //数据库
//连接本地数据库
$db = new DB_Sql();
$db->connect(DB_DATABASE, DB_HOST, DB_USER_NAME, DB_USER_PASS);
[ 使用方法 ]
//获取所有记录
$sql = "SELECT * FROM table1";
$all_record = $db->get_all($sql);
//获取一条
$sql = "SELECT * FROM table1 WHERE id = '1'";
$one_row = $db->get_one($sql);
//分页查询,提取20条记录
$sql = "SELECT * FROM table1";
$page_record = $db->limit_query($sql, $start=0, $offset=20, $order="ORDER BY id DESC");
//提取指定数目的记录
$sql = "SELECT * FROM table1";
$limit_record = $db->get_limit($sql,10);
//统计记录数,统计所有类型为学生的
$count = $db->count("table1", "id", "type = 'student'");
//插入一条记录
$info_array = array(
  "name"  => "heiyeluren",
  "type"  => "student",
  "age"  => "22",
  "gender" => "boy"
 );
$db->insert("table1", $info_array);
//更新一条记录
$info_array = array(
  "name"  => "heiyeluren",
  "type"  => "teacher",
  "age"  => "22",
  "gender" => "boy"
 );
$db->update("table1", $info_array, "name = 'heiyeluren'");
//删除记录
$db->delete("table1", "name = 'heiyeluren'");
//执行一条无结果集的SQL
$db->execute("DELETE FROM table1 WHERE name = 'heiyeluren'");
********************************************************************************************************/
/**
 * 文件: database.inc.php
 * 描述: 数据库操作类
 * 说明: 本库使用PHPLib DB库作为核心, 同时增加一些实用方法, 详细参考注释
 */
class DB_Sql 
{
  /* public: connection parameters */
  var $Host     = "";
  var $Database = "";
  var $User     = "";
  var $Password = "";
  /* public: configuration parameters */
  var $Auto_Free     = 1;     ## Set to 1 for automatic mysql_free_result()
  var $Debug         = 0;     ## Set to 1 for debugging messages.
  var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
  var $PConnect      = 0;     ## Set to 1 to use persistent database connections
  var $Seq_Table     = "db_sequence";
  /* public: result array and current row number */
  var $Record   = array();
  var $Row;
  /* public: current error number and error text */
  var $Errno    = 0;
  var $Error    = "";
  /* public: this is an api revision, not a CVS revision. */
  var $type     = "mysql";
  //var $revision = "1.2";
  /* private: link and query handles */
var $Link_ID = 0;
  var $Query_ID = 0;
  var $locked   = false;      ## 當我們有鎖定時設定為true

    $this->query($query);
  }
  /* public:一些瑣碎的報告*/
  function link_id(Link)> ;
  }
  函數query_id() {
    return $this->Query_ID;
  }     / * 處理預設值*/
   >Database;
    if ("" == $Host) 
      $Host       $User     = $this->User ;
    if ("" == $Password) 
      $Password = $this->Password;
   ( 0 == $this ->Link_ID ) {
      if(!$this->PConnect) {
        $this -> 選擇🎜>        $this ->Link_ID = mysql_pconnect($Host, $User, $Password); 
      }
      if  $User , $Password) 失敗。 ");
        返回0;
      }
      if (!@my ("無法使用資料庫".$Database );
        返回0;
      }
    }
    return $-p-cmd 🎜>  function free() {
      @mysql_free_result($this->Query_ID);
      $this->Query_ID = 0;
  {
    /* 請勿使用空白查詢,因為PHP4 會阻塞它們。當沒有被詢問的類別時,例如 在
       * 如此的情況下:「$db = new DB_Sql_Subclass;」
      nect() ) {
      return 0; /* 我們已經在connect() 中對此進行了投訴。 >Query_ID) {
      $this->free();
    }
    if ($this->Debug)
      printf("Debug: query = %s
n", $Query_String); ->Link_ID);
    $this->行   = 0;
    $this->Errno = mysql_errno();
->Query_ID) {
      $this->halt("無效的SQL:".$Query_String);
    }
    沒關係。
    return $this->Query_ID;
  }
  /* public:遍歷結果集*/
  function next_record()含$this->halt("next_record呼叫時沒有待處理的查詢。」);
      返回0;
    }
    >    $this->行   += 1;
    $this->Errno  = mysql_errno();
    Record) ;
    if (!$stat && $this->Auto_Free) {
      $this->free();
   :在結果集中的位置*/
  函數seek($pos = 0) {
    $status = @mysql_data_seek($this-> this->Row = $pos;
    else {
      $this->halt("seek($pos) 失敗:結果有".$this->num_rows()." 行。 >      /*半途而廢的嘗試來拯救世界,
       * 但是不要認為這個記錄在案,甚至
       * 理想的行為。
       */
      @mysql_data_seek($this->Query_ID, $this->num_rows());     }
    返回1;
  }
  /* public: 表鎖定*/
  「鎖定表”;
    if(is_array($table)) {
      while(list($key,$value) = each($table)) {
  」, "寫入", "低優先權寫入"
        if(is_int($key)) $key = $mode;
     $query .= str_replace(",", " $key, ", $value) . 「$鍵,」;
        } el ;
}
      }
      $query = substr($query, 0, -2);
    } = str_replace(", ", " $mode, ", $table) . 「$模式」;
    } else {
      $query .
    if(!$this->query($query)) {
      $this->halt("lock() 失敗。");
  ->locked = true;
    回傳true;
  }
  function unlock() {
    //在解鎖前設定為潛在的循環中 🎜>    if(!$this->query("解鎖表")) {
      $this->halt("unlock() 失敗。");   }
  /* public:評估結果(大小,寬度)*/
  function affected_rows() {
  >  函數 num_rows() {
    return @mysql_num_rows($this->Query_ID);
  }
  ;
  }
  /* public:簡寫符號*/
  function nf() {
    return $this->num_rows(); num_rows();
  }
  function f($Name) {
    if (isset($this->Record[$Name ];
    }
  }
  函數p($Name) {
    if (isset($this->Record[$Name]))姓名];
    }
  }
  /* public: 序號*/
  函數nextid($seq_name) {
  >    if(! $this->locked) {
      if($this->lock($this->Seq_Table)) {
    🎜>        $this- >halt("無法鎖定".$this->Seq_Table。 "-它已建立嗎?");
        返回0;
      }
    }  from % s where seq_name ='%s'",
               $this- > ;Seq_  if(!$this->query($q)) {
      $this- >halt('查詢在nextid 中失敗:'.$q);
      返回0;
    }
    /* ()) {
      $currentid = 0;
      $q = sprintf("插入 %s   $this->Seq_Table,
                 
          
                 )$    $this->halt('查詢在nextid 中失敗:'.$q);
        回 0;
      }
    }else {
      $currentid = $this->f("nextid");
    }
    $next =$$curcurrentid; s set nextid = ' %s' where seq_name = '%s'",
              $this->Seq             $seq_name) ;
    if(!$this->query($q)) {
      $this->halt('查詢在nextid 失敗:'.$q);
      返回0;
    }     }     if($locked) {
      $this->unlock();
    }  
  函數元資料($table = "", $full = false) {
    $count = 0;
   🎜>    /*
     * 由於與Table 的相容性問題,我們更改了Metadata() 的行為
     *;
     *  full 為false(預設):
     * $result[]:
     *   [0]["table "]  表名稱     *   [0]["len"]    欄位長度
     * [0][ 完整為true
     * $result[]:
     *   ["num_fields"] 元資料記錄數
     *   [0 ]["table 欄位表名
     * [0]["type"]   字段類型
     *   [0]["len"]    字段長度
      ta"][field name ] 名為「field name」的欄位的索引
     *   如果您有字段,則可以使用最後一個名稱,但沒有索引。
     *   檢定:  if (isset($result['meta']['myfield'])) { ...
     */
 ...
     */
 ...
     */
 
    // 結果
    if ($table) {
      $this->connect();    if ( !$id) {
        $this->halt("元資料查詢失敗。");
        返回false; else {
      $id = $this->Query_ID; 
      if (!$id) {
        $this->halt("未指定詢問。");
        回傳 false;
      }
    }
    $count = @my  }
    $count = @mysql_num_fields($id);
    //因為效能原因而做出這個 IF(一個 if 比 $count if fast)
    if (!$full) {
    if ) {
        $res[$i]["table"] = @mysql_field_table ($id, $i);
        $res[$i]["name"]  = @mysql_field_name  ($id, $i);
        $res[$i]["type"]  = @mysql_field_type  ($id, $i);
        $res[$i]["len"]   = @mysql_field_len   ($id, $i);
        $res[$i]["flags"] = @mysql_field_flags($id, $i);
      }
    } else { // 完整中
      $res["num_fields"]= $count;
      for ($i=0; $i        $res[$i][Ttable"] = @mysql_field_table ($id $
        $res[$i]["name"]  = @mysql_field_name  ($id, $i);
        $res[$i]["type"]  = @mysql_field_type  ($id, $i);
        $res[$i]["len"]   = @mysql_field_len   ($id, $i);
        $res[$i]["flags"] = @mysql_field_flags($id, $i);
        $res["meta"][$res[$i]["name"]] = $i;
      }
    }
    //當我們在表中被召喚時才釋放結果
    
    }
    回 $res;
  }
  /* public:找出可用的表格名稱*/
  function table_names() {
    $this->connect();
    $h = @mysql_query("顯示表", $this->Link_ID);
    $i = 0;
    while ($info = @mysql_fetch_row($h)) {
      $return[$i]["table_name 
      $return[$i]["tablespace_name"] = $this->資料庫;
      $return[$i]["database"]        = $this->資料庫;
      $i++;
    }
    @mysql_free_result($h);
    返回$return;
  }
  /* 導管:錯誤處理*/
  function halt($msg) {
    $this->Error = @mysql_error(this-my);
    $this->Errno = @mysql_errno($this->Link_ID);
    if ($this->locked) {
      $this->unlock();
    }
    if ($this->Halt_On_Error == "no")
      返回;
    if ($this->Halt_On_Error != "報告")
      die("會話已停止。");
  }
  function haltmsg($msg) {
    printf("Database error: %s
n", $msg);
    printf("MySQL Error: %s (%s)
n",
      $this->Errno,
      $this->Errno,
      $ }
 
 //----------------------------------
 // 模組:自訂函數
 // 功能: 部分實用的資料庫處理方法
 // 作者: heiyeluren
 // 時間: 2005-12-26
 //--------- -------------------------
 /**
  * 方法: execute($sql)
  * 功能: 執行一個SQL語句,主要針對沒有結果集回傳的SQL
  * 參數: $sql FROM table1 WHERE id = '1'")
  * 返回: 更新成功返回True,失敗返回False
  */
 function execute($sql)
 {
  if (empty($sql))
  {
   $this->error("Invalid parameter");
  🎜>  {
   return false;
  }
  return true;
 }
 /9 $this->query($sql);
  $result_array = array();
  while($this->next_record())
  {
  ;
  }
  if (count($result_array)  {
   return  
  * 方法: get_all($sql)
  * 功能: 取得SQL執行的所有記錄
  * 參數: $sql  使用的所有記錄* 回傳: 傳回包含所有查詢結果的二維陣列
  */
 function get_one($sql)
 {
  $this->query($sql);
  🎜>   return 0;
  }
  return $this->Record;
 }
 /**  $this->query($sql);
  $result_array = array();
  for ($i=0; $inext_record++); 🎜>  {
   $result_array[] = $this->Record;
  }
  if (count($result_鎠 }
  return $result_array;
 }
 /**
  * 方法: limit_query($sql, $start=0, $offset=20, $order="")
  * 功能: 為分頁的取得記錄

  * $sql  需要執行的SQL,例如: SELECT * FROM Table1
  * $start 記錄的開始數, 缺省為0
  * >  * $order 排序方式,缺省為空,例如:ORDER BY id DESC
  * 例如  需取得從0到10的記錄並且依照ID號倒排, get_limit("SELECT  , "ORDER BY id DESC");
  *
  * 回傳: 傳回包含所有查詢結果的二維陣列
  */
 函數 limit_query($sql, $start=0, $offset=20, $order="")
 {
 $$ = $ = $ 。 " $order  LIMIT $start,$offset";
  $this->query($sql);
  $結果= 數組();
  while($this->next_record())
  while($this->next_record())
{
   $result[] = $this->Record;
  }
  if (count($result)  回傳$結果;
 }
 /**
  * 方法: count($table,$field="*", $where="")
  * 功能: 統計表中資料總數
  *  表名
  * $field 需要統計的字段,默認為*
  * $where 條件語句,缺省為空
  *   按照ID為, "id", "user_age   *
  * 回傳: 回傳統計結果的數字
  */
 函數count($table,$field="*", $where="")
 {
  $$sql = ( $where) ?  "從$table 選擇COUNT($field)": "從$table WHERE $where 中選擇COUNT($field)");
  $result = $this->get_one($this->get_one($this->get_one($this) $sql);
  if (!is_array($result))
  {
   return 0;
  }   * 方法: insert($table,$dataArray)
  * 功能: 插入一筆記錄到表裡
  * 參數:  "張三", "user_age"=>"20歲");
  * 例如   例如插入用戶張三,年齡為20, insert("users", array("user_name"=>"張三", "user_age"=>"20歲"))
  *
  * : 插入記錄中成功返回記錄True,失敗回傳False
  */
 函數insert($table,$dataArray)
 {
  if (!is_array($dataArray) || count($dataArray)< ;=0)
>   $this->error("參數無效");
  }
  while(list($key,$val) = each($dataArray))
  {
 each($dataArray))
  {
  =$  key,";
   $value .= "'$val',";
  }
  $field = substr($field, 0, -1);  sub , 0, -1);
  $sql =「插入$表($欄位)值($值)」;
  if (!$this->query($sql))
  {
   return false;
  }
  回傳true;
 }
 /**
  * 方法: update($talbe, $dataArray, $where)
  * 功能: 更新一條記錄
  *  更新欄位和值的陣列,鍵為欄位名,值為欄位值,例如:array("user_name"=>"張三", "user_age"=>"20歲");
  * $where  條件語句
  * 例如   例如更新姓名為張三的使用者為李四,且年齡為21
  *    update("users",  array("user_name"=>"張三歲", "user_歲", array("user_name"=>"張三歲", "20歲", >"20age" "),  "user_name='張三'")
  *
  * 回傳: 更新成功回傳True,失敗回傳False
  */
 函數update($talbe, $dataArray, $where)
 {
  0)
  {
   $this->error("參數無效");
  }
  while(list($key,$val) = each($dataAr))    $value .= "$key = '$val',";
  }
  $value = substr($value, 0, -1); value WHERE $where」;
  if (!$this->query($sql))
  {
   return false )> **
  * 方法: delete($table, $where)
  * 功能: 刪除一筆記錄
  * 參數: 
  * $的條件語句
  * 例如   例如刪除使用者名稱為張三的用戶,delete("users",  "user_name='張三'")
  *  * 返回False
  */
 函數刪除($table, $where)
 {
  if (empty($where))
  ");
  }
  $sql = 「從$table WHERE $where 刪除」;
  if (!$this->query($  🎜>  }
  回傳true;
 }
 /**
  * 方法: error($msg="")
  * 功能: 顯示錯誤訊息後中止腳本
  * 參數: $msg */
 函數錯誤($msg="")
 {
 函數錯誤($msg="")
 {
 函數錯誤($msg="")
 {
 函數錯誤:$msgn
  退出();
 }
 /**
  * 方法:get_insert_id()
  * 功能:取得最後插入的ID
  * 參數: 無參數
  * 回傳:關閉成功返回ID,且返回失敗有沒有參數
  * 返回:關閉成功返回ID,返回失敗。*/
 函數get_insert_id() Link_ID);
 }
 /**
  * 方法:close()
  * 功能:關閉目前資料庫連線
  * 參數: 無參數
  * 返回:關閉成功返回true,失敗返回false
  * 返回:關閉成功返回true,失敗返回false
  * 返回:關閉成功返回true,失敗返回false
  * 返回:關閉成功返回true,失敗返回false
  */
 函數close()
 {
  return mysql_close($this->Link_ID)
  return mysql_close($this->Link_ID> } 
}
? >


以上就介紹了lineargradientbrush加強版phplib的DB類,包含了lineargradientbrush方面的內容,希望對PHP教程有興趣的朋友有所幫助。


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn