国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php連接mysql數據庫操作類

2024-09-04 11:44:18
字體:
來源:轉載
供稿:網友

這是一款比較全的mysql操作類,昨天寫了一個簡單的連接mysql數據庫代碼,相對于這個來說,那個是最簡單的了,這個是一款包括數據查詢,更新,刪除,等操作,實例代碼如下:

  1. class mysql{ 
  2.   private $db_host//數據庫主機 
  3.   private $db_user//數據庫用戶名 
  4.   private $db_pwd//數據庫密碼 
  5.   private $db_database//數據庫名 
  6.   private $conn//數據庫連接標識; 
  7.   private $sql//sql執行的語句 
  8.   private $result//query的資源標識符 
  9.   private $coding//數據庫編碼,gbk,utf8,gb2312 
  10.   private $show_error = true; //本地調試使用,打印錯誤 
  11.   /** 
  12.    * 構造函數 
  13.    * 
  14.    * @access public 
  15.    * @parameter string $db_host   數據庫主機 
  16.    * @parameter string $db_user   數據庫用戶名 
  17.    * @parameter string $db_pwd    數據庫密碼 
  18.    * @parameter string $db_database   數據庫名 
  19.    * @parameter string $coding    編碼 
  20.    * @return void 
  21.    */ 
  22.   public function __construct($db_host$db_user$db_pwd$db_database$coding){ 
  23.    $this->db_host = $db_host
  24.    $this->db_user = $db_user
  25.    $this->db_pwd =  $db_pwd
  26.    $this->db_database = $db_database
  27.    $this->coding = $coding
  28.    $this->connect(); 
  29.   } 
  30.   /** 
  31.    * 鏈接數據庫 
  32.    * 
  33.    * @access private 
  34.    * @return void 
  35.    */ 
  36.   private function connect(){ 
  37.    $this->conn = @mysql_connect($this->db_host,$this->db_user,$this->db_pwd); 
  38.    if(!$this->conn){ 
  39.     //show_error開啟時,打印錯誤 
  40.     if($this->show_error){ 
  41.      $this->show_error('錯誤提示:鏈接數據庫失敗!'); 
  42.     } 
  43.    } 
  44.    if(!@mysql_select_db($this->db_database, $this->conn)){ 
  45.     //打開數據庫失敗 
  46.     if($this->show_error){ 
  47.      $this->show_error('錯誤提示:打開數據庫失敗!'); 
  48.     } 
  49.    } 
  50.    if(!@mysql_query("set names $this->coding")){ 
  51.     //設置編碼失敗 
  52.     if($this->show_error){ 
  53.      $this->show_error('錯誤提示:設置編碼失敗!'); 
  54.     } 
  55.    } 
  56.   } 
  57.   /** 
  58.    * 可執行查詢添加修改刪除等任何sql語句 
  59.    * 
  60.    * @access public 
  61.    * @parameter string $sql   sql語句 
  62.    * @return resource  資源標識符 
  63.    */ 
  64.   public function query($sql){ 
  65.    $this->sql = $sql
  66.    $result = mysql_query($this->sql, $this->conn); 
  67.    if(!$result){ 
  68.     //query執行失敗,打印錯誤 
  69.     $this->show_error("錯誤的sql語句:"$this->sql); 
  70.    }else
  71.     //返回資源標識符 
  72.     return $this->result = $result
  73.    } 
  74.   } 
  75.   /** 
  76.    * 查詢mysql服務器中所有的數據庫 
  77.    * 
  78.    * @access public 
  79.    * @return void 
  80.    */ 
  81.   public function show_databases(){ 
  82.    $this->query("show databases"); 
  83.    //打印數據庫的總數 
  84.    echo "現有數據庫:" . mysql_num_rows($this->result); 
  85.    echo "<br />"
  86.    $i = 1; 
  87.    //循環輸出每個數據庫的名稱 
  88.    while($row=mysql_fetch_array($this->result)){ 
  89.     echo "$i $row[database]" . "<br />"
  90.     $i++; 
  91.    } 
  92.   } 
  93.   /** 
  94.    * 查詢數據庫下所有表名 
  95.    * 
  96.    * @access public 
  97.    * @return void 
  98.    */ 
  99.   public function show_tables(){ 
  100.    $this->query("show tables"); 
  101.    //打印表的總數 
  102.    echo "數據庫{$this->db_database}共有" . mysql_num_rows($this->result) . "張表:"
  103.    echo "<br />"
  104.    //構造數組下標,循環出數據庫所有表名 
  105.    $column_name = "tables_in_" . $this->db_database; 
  106.    $i = 1; 
  107.    //循環輸出每個表的名稱 
  108.    while($row=mysql_fetch_array($this->result)){ 
  109.     echo "$i $row[$column_name]" . "<br />"
  110.     $i++; 
  111.    } 
  112.   } 
  113.   /** 
  114.    * 取得記錄集,獲取數組-索引和關聯 
  115.    * 
  116.     * @access public 
  117.    * @return void 
  118.    */ 
  119.   public function fetch_array(){ 
  120.    return mysql_fetch_array($this->result); 
  121.   } 
  122.   /** 
  123.    * 簡化select查詢語句 
  124.    * 
  125.    * @access public 
  126.    * @parameter string $table  表名 
  127.    * @parameter string $field  字段名 
  128.    * @return resource 
  129.    */ 
  130.   public function findall($table$field = '*') { 
  131.    return $this->query("select $field from $table"); 
  132.   } 
  133.   /** 
  134.    * 簡化delete查詢語句 
  135.    * 
  136.    * @access public 
  137.    * @parameter string $table    表名 
  138.    * @parameter string $condition  查詢的條件 
  139.    * @return resource 
  140.    */ 
  141.   public function delete($table$condition) { 
  142.    return $this->query("delete from $table where $condition"); 
  143.   } 
  144.   /** 
  145.    * 簡化insert插入語句 
  146.    * 
  147.    * @access public 
  148.    * @parameter string $table  表名 
  149.    * @parameter string $field  字段名 
  150.    * @parameter string $value  插入值 
  151.    * @return resource 
  152.    */ 
  153.   public function insert($table$field$value) { 
  154.    return $this->query("insert into $table ($field) values ('$value')"); 
  155.   } 
  156.   /** 
  157.    * 簡化update插入語句 
  158.    * 
  159.    * @access public 
  160.    * @parameter string $table      表名 
  161.    * @parameter string $update_content  更新的內容 
  162.    * @parameter string $condition    條件 
  163.    * @return resource 
  164.    */ 
  165.   public function update($table$update_content$condition) { 
  166.    return $this->query("update $table set $update_content where $condition"); 
  167.   } 
  168.   /** 
  169.    * 取得上一步 insert 操作產生的 id 
  170.    * 
  171.    * @access public 
  172.    * @return integer 
  173.    */ 
  174.   public function insert_id() { 
  175.    return mysql_insert_id(); 
  176.   } 
  177.   /** 
  178.    * 計算結果集條數 
  179.    * 
  180.    * @access public 
  181.    * @return integer 
  182.    */ 
  183.   public function num_rows() { 
  184.    return mysql_num_rows($this->result); 
  185.   } 
  186.   /** 
  187.    * 查詢字段數量和字段信息 
  188.    * 
  189.    * @access public 
  190.    * @parameter string $table  表名 
  191.    * @return void 
  192.    */ 
  193.   public function num_fields($table) { 
  194.    $this->query("select * from $table"); 
  195.    echo "<br />"
  196.    //打印字段數 
  197.    echo "字段數:" . $total = mysql_num_fields($this->result); 
  198.    echo "<pre>"
  199.    //mysql_fetch_field() 函數從結果集中取得列信息并作為對象返回。 
  200.    for ($i = 0; $i < $total$i++) { 
  201.     print_r(mysql_fetch_field($this->result, $i)); 
  202.    } 
  203.    echo "</pre>"
  204.    echo "<br />"
  205.   } 
  206.   /** 
  207.    * 輸出sql語句錯誤信息 
  208.    * 
  209.    * @access public 
  210.    * @parameter string $message 提示信息 
  211.    * @return void 
  212.    */ 
  213.   public function show_error($message='',$sql=''){ 
  214.    echo "<fieldset>"
  215.    echo "<legend>錯誤信息提示:</legend><br />"
  216.    echo "<div style='font-size:14px; clear:both; font-family:verdana, arial, helvetica, sans-serif;'>"
  217.    //打印錯誤原因 
  218.    echo "錯誤原因:" . mysql_error() . "<br /><br />"
  219.    //打印錯誤信息 
  220.    //mysql_error() 函數返回上一個 mysql 操作產生的文本錯誤信息。 
  221.    echo "<div style='height:20px; background:#ff0000; border:1px #ff0000 solid'>"
  222.    echo "<font color='white'>" . $message . "</font>"
  223.    echo "</div>"
  224.    //打印錯誤sql語句 
  225.    echo "<font color='red'><pre>" . $sql . "</pre></font>"
  226.    echo "</div>";//開源代碼Vevb.com 
  227.    echo "</fieldset>"
  228.   } 
  229.  } 
  230. //使用方法 
  231. $mysql = new mysql($dbhost$dbuser$dbpwd$dbname$coding);

上一篇:mysql 分頁類

下一篇:mysql數據庫連接類

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 罗江县| 延吉市| 巫山县| 丽水市| 印江| 出国| 古蔺县| 榕江县| 淄博市| 灵台县| 铜陵市| 定结县| 三河市| 美姑县| 南宫市| 徐水县| 依兰县| 蒙城县| 葫芦岛市| 南充市| 开平市| 盐源县| 义乌市| 疏勒县| 兴山县| 灌阳县| 凌海市| 盐津县| 若羌县| 鄂托克前旗| 泸水县| 万州区| 东阳市| 乌拉特中旗| 定远县| 沁水县| 白玉县| 达日县| 七台河市| 威远县| 惠水县|