




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、<?php/*文件名】: mySql作用】:mysql 數(shù)據(jù)庫操作類作者】 :Riyan版本】 :version 1.6修改日期】: 2009/09/10*/class mysql private $host; private $user; private $pwd; private $db; private $conn; private $result; private $sql; private $row; private $char;/ 數(shù)據(jù)庫主機/ 數(shù)據(jù)庫用戶名/ 數(shù)據(jù)庫密碼/ 數(shù)據(jù)庫名/ 數(shù)據(jù)庫連接標識/ 執(zhí)行 query 命令的結(jié)果資源標識/ sql 執(zhí)行語句/ 返回的條目數(shù)
2、/ 數(shù)據(jù)庫編碼, GBK,UTF8,gb2312private $error_log = true; / 是否開啟錯誤記錄private $show_error = true; / 測試階段,顯示所有錯誤 ,具有安全隱患 , 默認關閉private $is_error = false; /發(fā)現(xiàn)錯誤是否立即終止,默認true,建議不啟用,因為當有問題時 用戶什么也看不到是很苦惱的private $pageNo = 1; private $pageAll = 0; private $rsAll = 0; private $pageSize;/ 當前頁/ 總頁數(shù)/ 總記錄/ 每頁顯示記錄條數(shù)/*函數(shù)
3、名: _construct($host,$user,$pwd,$database,$conn,$char) 作 用:構造函數(shù)參 數(shù): $host (數(shù)據(jù)庫主機 )$user (數(shù)據(jù)庫用戶名 )$pwd ( 數(shù)據(jù)庫密碼 )$db(數(shù)據(jù)庫名 )$conn (數(shù)據(jù)庫連接標識 ) $char (數(shù)據(jù)庫編碼 )返回值:無實 例:無*/public function_construct($host='localhost',$user='root',$pwd='',$db='ecshop',$char="gbk",$conn
4、="conn")$this->host =$host;$this->user =$user;$this->pwd= $pwd;$this->db =$db;$this->conn =$conn;$this->char =$char;$this->connect();/ 數(shù)據(jù)庫連接private function connect()if($this->conn="pconn") $this->conn=mysql_pconnect($this->host,$this->user,$this-
5、>pwd); / 永久鏈接else $this->conn=mysql_connect($this->host,$this->user,$this->pwd); / 臨時鏈接if ($this->show_error)if(!$this->conn) $this->show_error(" 無法連接服務器 !");if(!$this->select_db($this->db) $this->show_error(" 無法連接數(shù)據(jù)庫: ",$this->db);mysql_query(
6、"SET NAMES $this->char");mysql_query("SET CHARACTER_SET_CLIENT='$this->char'");mysql_query("SET CHARACTER_SET_RES ULTS='$this->char'");/ 數(shù)據(jù)庫選擇public function select_db($db)return mysql_select_db($db, $this->conn);/*函數(shù)名: mysql_server($num)作 用:
7、取得 MySQL 服務器信息參 數(shù): $num( 信息值 )返回值:字符串實 例:無*/public function mysql_server($num='')switch ($num)case 1:return mysql_get_server_info(); / 取得 MySQL 服務器信息break;case 2:return mysql_get_host_info(); / 取得 MySQL 主機信息break;取得 MySQL 客戶端信息取得 MySQL 協(xié)議信息取得 MySQL 版本信息case 3:return mysql_get_client_info();
8、/ break;case 4:return mysql_get_proto_info(); /break;default:return mysql_get_client_info(); /*函數(shù)名: query($sql)作 用:數(shù)據(jù)庫執(zhí)行語句,可執(zhí)行查詢添加修改刪除等任何 sql 語句 參 數(shù): $sql(sql 語句 )返回值:布爾實 例:無*/ public function query($sql)if($sql= "")$this->show_error("sql 語句錯誤: ","sql 查詢語句為空 "); $thi
9、s->sql=$sql;$result=mysql_query($this->sql,$this->conn);if(!$result)sqlif($this->show_error) $this->show_error("sql 語句錯誤: ",$this->sql); / 調(diào)試中使用, 語句出錯時會自動打印出來else$this->result=$result;return $result;/*函數(shù)名: create_db($database_name) 作 用:創(chuàng)建添加新的數(shù)據(jù)庫 參 數(shù): $db_name( 數(shù)據(jù)庫名稱 )
10、返回值:無 實 例:無*/public function create_db($db_name)$this->query("CREA TE DA TABASE ".$db_name); / 查詢服務器所有數(shù)據(jù)庫public function show_db()$this->query("show databases");echo " 現(xiàn)有數(shù)據(jù)庫: ".$this->num_rows()."<br />"$i=1;while($row=$this->fetch_array()ech
11、o $i.".".$rowDatabase."<br />"$i+;/ 以數(shù)組形式返回主機中所有數(shù)據(jù)庫名 public function db_list() $rsPtr=mysql_list_dbs($this->conn);$i=0;$cnt=mysql_num_rows($rsPtr);while($i < $cnt)$rs=mysql_db_name($rsPtr,$i);$i+;return $rs;/ 查詢數(shù)據(jù)庫下所有的表public function show_tables($db_name)$this->qu
12、ery("show tables");echo " 現(xiàn)有數(shù)據(jù)庫: ".$amount=$this->num_rows(); echo "<br />"$i=1;while($row=$this->fetch_array() $Fileds="Tables_in_".$db_name; echo "$i $row$Fileds" echo "<br />"$i+;/*函數(shù)名: num_fields($Table) 作 用:查詢字段數(shù)量參 數(shù):
13、 $Table( 數(shù)據(jù)庫表名 ) 返回值:字符串 實 例: $DB->num_fields("mydb") */ public function num_fields($Table)$this->query("SELECT * FROM $Table");echo "<br />"echo " 字段數(shù): ".$total=mysql_num_fields($this->result);echo "<pre>"for ($i=0; $i<$total
14、; $i+) print_r(mysql_fetch_field($this->result,$i);echo "</pre><br />"/*函數(shù)名: Get($Table,$Fileds,$Condition) 作 用:查詢數(shù)據(jù)參 數(shù): $Table( 表名 )$Fileds( 字段名,默認為所有 )$Condition( 查詢條件 ) 返回值:無 實 例: $DB->Get("mydb","user,password","order by id desc") */ publ
15、ic function Get($Table,$Fileds="*",$Condition="") if (!$Fileds | empty($Fileds)$Fileds="*" return $this->query("SELECT $Fileds FROM $Table $Condition");public function GetOnce($Table,$Fileds="*",$Condition="") if (!$Fileds | empty($Filed
16、s)$Fileds="*" return $this->query("SELECT $Fileds FROM $Table $Condition LIMIT 0,1");/*函數(shù)名: Add($Table,$Fileds,$V alue) 作 用:添加數(shù)據(jù)參 數(shù): $Table( 表名 ) $Fileds( 字段名 )$Value(對應值)返回值:布爾 實 例: $DB->Add("mydb","user,password","'admin','123456'&
17、quot;) */ public function Add($Table,$Fileds,$V alue)return $this->query("INSERT INTO $Table ($Fileds) VALUES ($V alue)");/*函數(shù)名: Set($Table,$Content,$Condition) 作 用:更改數(shù)據(jù)參數(shù):$Table(表名)$Content(數(shù)據(jù)內(nèi)容) $Condition( 更改條件 )返回值:布爾實 例: $DB->Set("mydb","user='admin',passw
18、ord='123456'","where id=1") */ public function Set($Table,$Content,$Condition="")return $this->query("UPDA TE $Table SET $Content $Condition");/*函數(shù)名: Del($Table,$Condition) 作 用:刪除數(shù)據(jù) 參 數(shù): $Table( 表名 ) $Condition( 刪除條件 )返回值:布爾實 例: $DB->Del("mydb&qu
19、ot;,"id=1") */ public function Del($Table,$Condition="")return $this->query("DELETE FROM $Table WHERE $Condition");/ 取得結(jié)果數(shù)據(jù)public function result()return mysql_result($str);/ 取得記錄集 , 獲取數(shù)組 - 索引和關聯(lián) ,使用 $row'content'public function fetch_array()return mysql_fetc
20、h_array($this->result);/獲取關聯(lián)數(shù)組,使用$row'字段名'public function fetch_assoc()return mysql_fetch_assoc($this->result);/ 獲取數(shù)字索引數(shù)組 ,使用 $row0,$row1,$row2public function fetch_row()return mysql_fetch_row($this->result);/ 獲取對象數(shù)組 , 使用 $row->contentpublic function fetch_obj()return mysql_fetch
21、_object($this->result);/ 取得上一步 INSERT 操作產(chǎn)生的 IDpublic function insert_id()return mysql_insert_id();/ 指向確定的一條數(shù)據(jù)記錄public function data_seek($id) if($id>0)$id=$id-1; if(!mysql_data_seek($this->result,$id)$this->show_error("sql 語句有誤: ", "指定的數(shù)據(jù) 為空 ");return $this->result
22、;/ 根據(jù) select 查詢結(jié)果計算結(jié)果集條數(shù)public function num_rows() if($this->result=null) if($this->show_error)$this->show_error("sql 語句錯誤 "," 暫時為空,沒有任何內(nèi)容! else return mysql_num_rows($this->result);/ 根據(jù) insert,update,delete 執(zhí)行結(jié)果取得影響行數(shù)public function affected_rows()return mysql_affected_ro
23、ws();/ 獲取地址欄參數(shù)public function getQuery()foreach($_GET as $key => $value)$list .= $key."=".$value;return $list?"?".implode("&", $list):""/*函數(shù)名: getPage($Table,$Fileds,$Condition,$pageSize)作 用:獲取分頁信息參 數(shù): $Table( 表名 )$Fileds(字段)$Condition( 查詢條件 )$pageS
24、ize(每頁記錄條數(shù))返回值:字符串實 例:無 */public function getPage($Table,$Fileds="*",$Condition="",$pageSize=10) $this->pageSize=intval($pageSize);if(isset($_GET"Page")intval($_GET"Page")$this->pageNo=intval($_GET"Page");if (empty($Fileds)$Fileds="*"
25、$this->Get($Table,"*",$Condition); $this->rsAll=$this->num_rows();if ($this->rsAll>0)$this->pageAll=ceil($this->rsAll/$this->pageSize);if ($this->pageNo<1)$this->pageNo =1;if ($this->pageNo>$this->pageAll)$this->pageNo=$this->pageAll;$sql=&qu
26、ot;SELECT $Fileds FROM $Table $Condition".$this->limit(true); $this->query($sql);");&&return $this->rsAll;/ 分頁 limit 語句public function limit($str=false) $n=($this->pageNo-1)*$this->pageSize; return $str ? " LIMIT ".$n.",".$this->pageSize:$n;/ 顯
27、示分頁public function showPage()$pageNav=""if($this->pageAll>1)$pageNav.="<div class="page">".chr(10); unset($_GET"Page");$url=$this->getQuery();$url=empty($url)?"?Page=":$url."&Page="if ($this->pageNo>1)$pageNav.=
28、"<a href="".$url."1"> 首頁 </a>".chr(10);$pageNav.="<a href="".$url.($this->pageNo-1).""> 上頁 </a>".chr(10);if ($this->pageAll<5)$n=array(1,$this->pageAll);elseif($this->pageNo>6)$n=array($this->pag
29、eNo)-2,($this->pageNo)+2);if ($n1>$this->pageAll) $n=array($this->pageAll)-4,$this->pageAll);else$n=array(1,5);for ($n0;$n0<=$n1;$n0+)$stat = $this->pageNo=$n0 ? " class="curr"":" href="".$url.$n0."""$pageNav.="<a title=&
30、quot;第".$n0."頁"".$stat.">".$n0."</a>".chr(10);if ($this->pageNo<$this->pageAll)$pageNav.="<a href="".$url.($this->pageNo+1).""> 下頁 </a>".chr(10);$pageNav.="<a href="".$url.$this-&
31、gt;pageAll.""> 尾頁 </a>".chr(10);$pageNav.=" 頁 次 :<b>".$this->pageNo."</b>/".$this->pageAll."<b>".$this->rsAII."</b> 條記錄 <b>".$this->pageSize."</b> 條/頁</div>".chr(10);return
32、 $pageNav;/ 獲得客戶端真實的 IP 地址public function getip()if($_SERVER"HTTP_X_FORWARDED_FOR")$ip$_SERVER"HTTP_X_FORWARDED_FOR"elseif ($_SERVER"HTTP_CLIENT_IP") $ip = $_SERVER"HTTP_CLIENT_IP"elseif ($_SERVER"REMOTE_ADDR") $ip = $_SERVER"REMOTE_ADDR"el
33、seif (getenv("HTTP_X_FORWARDED_FOR") $ip = getenv("HTTP_X_FORWARDED_FOR");elseif (getenv("HTTP_CLIENT_IP") $ip = getenv("HTTP_CLIENT_IP");elseif (getenv("REMOTE_ADDR") $ip = getenv("REMOTE_ADDR");else $ip = NULL;return $ip;/*函數(shù)名: show_error
34、($message,$sql)作 用:輸出顯示錯誤信息參數(shù):$message錯誤信息)$sql(sql 語句 )返回值:字符串實 例:無 */ public function show_error($message="",$sql="")if(!$sql)echo "<font color='red'>".$message."</font>"elseecho "<fieldset>"echo "<legend>錯誤信息提示
35、 </legend>"echo "<div style='font-size:14px; font-family:V erdana, Arial, Helvetica, sans-serif;'> 錯誤原 因: ".mysql_error()."<br /><br />"echo "<div style='line-height:20px; background:#F00; border:1px solid #F00; color:#FFF;'>
36、;".$message."</div>"echo "<font color='red'>".$sql."</font><br />"if($this->error_log)$ip=$this->getip();$message=$message."rn".$this->sql."rn 客 戶 IP:".$ip."rn 時 間 :".date("Y-m-d H:i:s"
37、;)."rnrn"$filename=date("Y -m-d").".txt"$file_path="./error/".$filename;if(!file_exists("./error")if(!mkdir("./error",0777) /建立文件夾,默認的mode是0777,意味著最大可能的訪問權die("upload files directory does not exist and creation failed");if(!file_exists($file_path)fopen($file_path,"w+"); / 建立 txt 文件if (is_writable($file_path) / 確定文件存在并且可寫if (!$handle=fopen($file_path, 'a') /使用添加模式打開 $filename,文件指針將會在文件的開頭echo "_不能打開文件
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 農(nóng)業(yè)產(chǎn)業(yè)園區(qū)投資合作協(xié)議
- 建筑工程玻璃貼膜合同
- 股權激勵保密協(xié)議書
- 公關攝影師聘用合同
- 合伙美容院合同
- 財產(chǎn)分割離婚協(xié)議
- 工業(yè)互聯(lián)網(wǎng)平臺建設與優(yōu)化方案
- 裝修施工安全合同協(xié)議書
- 醫(yī)院大樓裝修工程承包協(xié)議書
- 以車抵押借款合同
- 《事故汽車常用零部件修復與更換判別規(guī)范》
- 2024-2030年中國酒類流通行業(yè)發(fā)展動態(tài)及投資盈利預測研究報告
- 物業(yè)管理如何實現(xiàn)降本增效
- DL-T825-2021電能計量裝置安裝接線規(guī)則
- 信息科技重大版 七年級下冊 互聯(lián)網(wǎng)應用與創(chuàng)新 第一單元單元教學設計 互聯(lián)網(wǎng)創(chuàng)新應用
- 2024年興業(yè)銀行股份有限公司校園招聘考試試題及參考答案
- 2024智慧城市城市交通基礎設施智能監(jiān)測技術要求
- 2024年湖南環(huán)境生物職業(yè)技術學院單招職業(yè)適應性測試題庫及參考答案
- 專題06 壓強計算(壓強與浮力結(jié)合題)-上海市2024年中考復習資料匯編(培優(yōu)專用)【解析版】
- 湖北省崇陽縣浪口溫泉地熱田地熱資源開發(fā)利用與生態(tài)復綠方案
- 《工程建設標準強制性條文電力工程部分2023年版》
評論
0/150
提交評論