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

首頁 > 語言 > PHP > 正文

實用mysql數據庫連接類

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

這是一款PHP與mysql數據庫連接文件代碼,如果你正在找這樣功能的代碼,可以進來看看,非常完整文件,實例代碼如下:

  1. class mysql { 
  2.  private $db_host;     //主機地址 
  3.  private $db_user;     //用戶名 
  4.  private $db_pass;     //連接密碼 
  5.  private $db_name;     //名稱 
  6.  private $db_charset;  //編碼 
  7.  
  8.  private $conn
  9.  private $query_id;   //用于判斷sql語句是否執行成功 
  10.  private $result;     //結果集 
  11.  private $num_rows;   //結果集中行的數目,僅對select有效 
  12.  private $insert_id;  //上一步 insert 操作產生的 id 
  13.  
  14. // 構造/析構函數 
  15.  function __construct ($db_host,$db_user,$db_pass,$db_name,$db_charset,$conn) { 
  16.  $this->db_host = $db_host ; 
  17.  $this->db_user = $db_user ; 
  18.  $this->db_pass = $db_pass ; 
  19.  $this->db_name = $db_name ; 
  20.  $this->db_charset = $db_charset ; 
  21.  $this->conn = $conn ; 
  22.  $this->connect(); 
  23.  } 
  24.  
  25.  function __destruct () { 
  26.  @mysql_close($this->conn); 
  27.  } 
  28.  
  29. // 連接/選擇數據庫 
  30.  public function connect () { 
  31.  if ($this->conn == 'pconn') { 
  32.   @$this->conn = mysql_pconnect($this->db_host,$this->db_user,$this->db_pass); 
  33.  } else { 
  34.   @$this->conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass); 
  35.  } 
  36.  if (!$this->conn) { 
  37.   $this->show_error('數據庫-連接失敗:用戶名或密碼錯誤!'); 
  38.  } 
  39.  if (!@mysql_select_db($this->db_name,$this->conn)) { 
  40.   $this->show_error("數據庫-選擇失敗:數據庫 $this->db_name 不可用"); 
  41.  } 
  42.  mysql_query("set names $this->db_charset"); 
  43.  return $this->conn; 
  44.  } 
  45.  
  46. // query方法 
  47.  public function query ($sql) { 
  48.  if ($this->query_id) $this->free_result(); 
  49.  $this->query_id = @mysql_query($sql,$this->conn); 
  50.  if (!$this->query_id) $this->show_error("sql語句 <b>"$sql"</b> 執行時遇到錯誤"); 
  51.  return $this->query_id; 
  52.  } 
  53.  
  54. // 查詢所有 
  55.  public function findall ($table_name) { 
  56.  $this->query("select * from $table_name"); 
  57.  } 
  58.  
  59. // mysql_fetch_array 
  60.  public function fetch_array () { 
  61.  if ($this->query_id) { 
  62.   $this->result = mysql_fetch_array($this->query_id); 
  63.   return $this->result; 
  64.  } 
  65.  } 
  66.  
  67. // ...... 
  68.  
  69.  public function fetch_assoc () { 
  70.  if ($this->query_id) { 
  71.   $this->result = mysql_fetch_assoc($this->query_id); 
  72.   return $this->result; 
  73.  } 
  74.  } 
  75.  
  76.  public function fetch_row () { 
  77.  if ($this->query_id) { 
  78.   $this->result = mysql_fetch_row($this->query_id); 
  79.   return $this->result; 
  80.  } 
  81.  } 
  82.  
  83.  public function fetch_object () { 
  84.  if ($this->query_id) { 
  85.   $this->result = mysql_fetch_object($this->query_id); 
  86.   return $this->result; 
  87.  } 
  88.  } 
  89.  
  90. // 獲取 num_rows 
  91.  public function num_rows () { 
  92.  if ($this->query_id) { 
  93.   $this->num_rows = mysql_num_rows($this->query_id); 
  94.   return $this->num_rows; 
  95.  } 
  96.  } 
  97.  
  98. // 獲取 insert_id 
  99.  public function insert_id () { 
  100.  return $this->insert_id = mysql_insert_id(); 
  101.  } 
  102.  
  103. // 顯示共有多少張表 
  104.  public function show_tables () { 
  105.  $this->query("show tables"); 
  106.  if ($this->query_id) { 
  107.   echo "數據庫 $this->db_name 共有 ".$this->num_rows($this->query_id)." 張表<br/>"
  108.   $i = 1; 
  109.   while ($row = $this->fetch_array($this->query_id)){ 
  110.     echo "$i -- $row[0]<br/>"
  111.     $i ++; 
  112.   } 
  113.  } 
  114.  } 
  115.  
  116. // 顯示共有多少個數據庫 
  117.  public function show_dbs(){ 
  118.  $this->query("show databases"); 
  119.  if ($this->query_id) { 
  120.   echo "共有數據庫 ".$this->num_rows($this->query_id)." 個<br/>"
  121.   $i = 1; 
  122.   while ($this->row = $this->fetch_array($this->query_id)){ 
  123.     echo "$i -- ".$this->row[database]."<br />"
  124.     $i ++; 
  125.   } 
  126.  } 
  127.  } 
  128.  
  129. // 刪除數據庫:返回刪除結果 
  130.  public function drop_db ($db_name='') { 
  131.   if ($db_name == '') { 
  132.    $db_name = $this->db_name;//默認刪除當前數據庫 
  133.   $this->query("drop database $db_name"); 
  134.  }else { 
  135.   $this->query("drop database $db_name"); 
  136.  } 
  137.  if ($this->query_id) { 
  138.   return "數據庫 $db_name 刪除成功"
  139.  }else { 
  140.   $this->show_error("數據庫 $db_name 刪除失敗"); 
  141.  } 
  142.  
  143. // 刪除數據表:返回刪除結果 
  144.  public function drop_table ($table_name) { 
  145.  $this->query("drop table $table_name"); 
  146.   if ($this->query_id) { 
  147.   return "數據表 $table_name 刪除成功"
  148.  }else { 
  149.   $this->show_error("數據表 $table_name 刪除失敗"); 
  150.  } 
  151.  
  152.  
  153. // 創建數據庫 
  154. public function create_db ($db_name) { 
  155.  $this->query("create database $db_name"); 
  156.  if($this->query_id){ 
  157.   return "數據庫 $db_name 創建成功"
  158.  }else { 
  159.   $this->show_error("數據庫 $db_name 創建失敗"); 
  160.  } 
  161.  
  162. // 獲取數據庫版本 
  163.  public function get_info(){ 
  164.  echo mysql_get_server_info(); 
  165.  } 
  166.  
  167. // 顯示錯誤信息 
  168.  public function show_error ($msg) { 
  169.  $errinfo = mysql_error(); 
  170.  echo "錯誤:$msg <br/> 返回:$errinfo<p>"
  171.  }//開源代碼Vevb.com 
  172.  
  173. // 釋放內存 
  174.  public function free_result () { 
  175.  if ( @mysql_free_result($this->query_id) ) 
  176.  unset ($this->result); 
  177.  $this->query_id = 0; 
  178.  } 
  179.  
  180. // end class

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 香格里拉县| 邓州市| 荥经县| 樟树市| 博罗县| 定襄县| 连江县| 正宁县| 彩票| 东港市| 兴义市| 和龙市| 大厂| 新余市| 长顺县| 乌恰县| 浙江省| 临湘市| 五寨县| 邢台市| 忻州市| 伊金霍洛旗| 溧水县| 迭部县| 新丰县| 和林格尔县| 米泉市| 上思县| 南投市| 高邮市| 东安县| 五大连池市| 郴州市| 定西市| 新巴尔虎左旗| 黑水县| 连山| 赣州市| 乌恰县| 太谷县| 广丰县|