PHP数据库操作分页类
生活随笔
收集整理的這篇文章主要介紹了
PHP数据库操作分页类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MySQL數據庫操作類:
1 <?php 2 class mysql { 3 private $db_host; //數據庫主機 4 private $db_user; //數據庫用戶名 5 private $db_pwd; //數據庫用戶名密碼 6 private $db_database; //數據庫名 7 private $conn; //數據庫連接標識; 8 private $result; //執行query命令的結果資源標識 9 private $sql; //sql執行語句 10 private $row; //返回的條目數 11 private $coding; //數據庫編碼,GBK,UTF8,gb2312 12 private $bulletin = true; //是否開啟錯誤記錄 13 private $show_error = true; //測試階段,顯示所有錯誤,具有安全隱患,默認關閉 14 private $is_error = false; //發現錯誤是否立即終止,默認true,建議不啟用,因為當有問題時用戶什么也看不到是很苦惱的 15 16 /*構造函數*/ 17 public function __construct($db_host, $db_user, $db_pwd, $db_database, $conn, $coding) { 18 $this->db_host = $db_host; 19 $this->db_user = $db_user; 20 $this->db_pwd = $db_pwd; 21 $this->db_database = $db_database; 22 $this->conn = $conn; 23 $this->coding = $coding; 24 $this->connect(); 25 } 26 27 /*數據庫連接*/ 28 public function connect() { 29 if ($this->conn == "pconn") { 30 //永久鏈接 31 $this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd); 32 } else { 33 //即使鏈接 34 $this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd); 35 } 36 37 if (!mysql_select_db($this->db_database, $this->conn)) { 38 if ($this->show_error) { 39 $this->show_error("數據庫不可用:", $this->db_database); 40 } 41 } 42 mysql_query("SET NAMES $this->coding"); 43 } 44 45 /*數據庫執行語句,可執行查詢添加修改刪除等任何sql語句*/ 46 public function query($sql) { 47 if ($sql == "") { 48 $this->show_error("SQL語句錯誤:", "SQL查詢語句為空"); 49 } 50 $this->sql = $sql; 51 52 $result = mysql_query($this->sql, $this->conn); 53 54 if (!$result) { 55 //調試中使用,sql語句出錯時會自動打印出來 56 if ($this->show_error) { 57 $this->show_error("錯誤SQL語句:", $this->sql); 58 } 59 } else { 60 $this->result = $result; 61 } 62 return $this->result; 63 } 64 65 /*創建添加新的數據庫*/ 66 public function create_database($database_name) { 67 $database = $database_name; 68 $sqlDatabase = 'create database ' . $database; 69 $this->query($sqlDatabase); 70 } 71 72 /*查詢服務器所有數據庫*/ 73 //將系統數據庫與用戶數據庫分開,更直觀的顯示? 74 public function show_databases() { 75 $this->query("show databases"); 76 echo "現有數據庫:" . $amount = $this->db_num_rows($rs); 77 echo "<br />"; 78 $i = 1; 79 while ($row = $this->fetch_array($rs)) { 80 echo "$i $row[Database]"; 81 echo "<br />"; 82 $i++; 83 } 84 } 85 86 //以數組形式返回主機中所有數據庫名 87 public function databases() { 88 $rsPtr = mysql_list_dbs($this->conn); 89 $i = 0; 90 $cnt = mysql_num_rows($rsPtr); 91 while ($i < $cnt) { 92 $rs[] = mysql_db_name($rsPtr, $i); 93 $i++; 94 } 95 return $rs; 96 } 97 98 /*查詢數據庫下所有的表*/ 99 public function show_tables($database_name) { 100 $this->query("show tables"); 101 echo "現有數據庫:" . $amount = $this->db_num_rows($rs); 102 echo "<br />"; 103 $i = 1; 104 while ($row = $this->fetch_array($rs)) { 105 $columnName = "Tables_in_" . $database_name; 106 echo "$i $row[$columnName]"; 107 echo "<br />"; 108 $i++; 109 } 110 } 111 112 /* 113 mysql_fetch_row() array $row[0],$row[1],$row[2] 114 mysql_fetch_array() array $row[0] 或 $row[id] 115 mysql_fetch_assoc() array 用$row->content 字段大小寫敏感 116 mysql_fetch_object() object 用$row[id],$row[content] 字段大小寫敏感 117 */ 118 119 /*取得結果數據*/ 120 public function mysql_result_li() { 121 return mysql_result($str); 122 } 123 124 /*取得記錄集,獲取數組-索引和關聯,使用$row['content'] */ 125 public function fetch_array() { 126 return mysql_fetch_array($this->result); 127 } 128 129 //獲取關聯數組,使用$row['字段名'] 130 public function fetch_assoc() { 131 return mysql_fetch_assoc($this->result); 132 } 133 134 //獲取數字索引數組,使用$row[0],$row[1],$row[2] 135 public function fetch_row() { 136 return mysql_fetch_row($this->result); 137 } 138 139 //獲取對象數組,使用$row->content 140 public function fetch_Object() { 141 return mysql_fetch_object($this->result); 142 } 143 144 //簡化查詢select 145 public function findall($table) { 146 $this->query("SELECT * FROM $table"); 147 } 148 149 //簡化查詢select 150 public function select($table, $columnName = "*", $condition = '', $debug = '') { 151 $condition = $condition ? ' Where ' . $condition : NULL; 152 if ($debug) { 153 echo "SELECT $columnName FROM $table $condition"; 154 } else { 155 $this->query("SELECT $columnName FROM $table $condition"); 156 } 157 } 158 159 //簡化刪除del 160 public function delete($table, $condition, $url = '') { 161 if ($this->query("DELETE FROM $table WHERE $condition")) { 162 if (!empty ($url)) 163 $this->Get_admin_msg($url, '刪除成功!'); 164 } 165 } 166 167 //簡化插入insert 168 public function insert($table, $columnName, $value, $url = '') { 169 if ($this->query("INSERT INTO $table ($columnName) VALUES ($value)")) { 170 if (!empty ($url)) 171 $this->Get_admin_msg($url, '添加成功!'); 172 } 173 } 174 175 //簡化修改update 176 public function update($table, $mod_content, $condition, $url = '') { 177 //echo "UPDATE $table SET $mod_content WHERE $condition"; exit(); 178 if ($this->query("UPDATE $table SET $mod_content WHERE $condition")) { 179 if (!empty ($url)) 180 $this->Get_admin_msg($url); 181 } 182 } 183 184 /*取得上一步 INSERT 操作產生的 ID*/ 185 public function insert_id() { 186 return mysql_insert_id(); 187 } 188 189 //指向確定的一條數據記錄 190 public function db_data_seek($id) { 191 if ($id > 0) { 192 $id = $id -1; 193 } 194 if (!@ mysql_data_seek($this->result, $id)) { 195 $this->show_error("SQL語句有誤:", "指定的數據為空"); 196 } 197 return $this->result; 198 } 199 200 // 根據select查詢結果計算結果集條數 201 public function db_num_rows() { 202 if ($this->result == null) { 203 if ($this->show_error) { 204 $this->show_error("SQL語句錯誤", "暫時為空,沒有任何內容!"); 205 } 206 } else { 207 return mysql_num_rows($this->result); 208 } 209 } 210 211 // 根據insert,update,delete執行結果取得影響行數 212 public function db_affected_rows() { 213 return mysql_affected_rows(); 214 } 215 216 //輸出顯示sql語句 217 public function show_error($message = "", $sql = "") { 218 if (!$sql) { 219 echo "<font color='red'>" . $message . "</font>"; 220 echo "<br />"; 221 } else { 222 echo "<fieldset>"; 223 echo "<legend>錯誤信息提示:</legend><br />"; 224 echo "<div style='font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;'>"; 225 echo "<div style='height:20px; background:#000000; border:1px #000000 solid'>"; 226 echo "<font color='white'>錯誤號:12142</font>"; 227 echo "</div><br />"; 228 echo "錯誤原因:" . mysql_error() . "<br /><br />"; 229 echo "<div style='height:20px; background:#FF0000; border:1px #FF0000 solid'>"; 230 echo "<font color='white'>" . $message . "</font>"; 231 echo "</div>"; 232 echo "<font color='red'><pre>" . $sql . "</pre></font>"; 233 $ip = $this->getip(); 234 if ($this->bulletin) { 235 $time = date("Y-m-d H:i:s"); 236 $message = $message . "/r/n$this->sql" . "/r/n客戶IP:$ip" . "/r/n時間 :$time" . "/r/n/r/n"; 237 238 $server_date = date("Y-m-d"); 239 $filename = $server_date . ".txt"; 240 $file_path = "error/" . $filename; 241 $error_content = $message; 242 //$error_content="錯誤的數據庫,不可以鏈接"; 243 $file = "error"; //設置文件保存目錄 244 245 //建立文件夾 246 if (!file_exists($file)) { 247 if (!mkdir($file, 0777)) { 248 //默認的 mode 是 0777,意味著最大可能的訪問權 249 die("upload files directory does not exist and creation failed"); 250 } 251 } 252 253 //建立txt日期文件 254 if (!file_exists($file_path)) { 255 256 //echo "建立日期文件"; 257 fopen($file_path, "w+"); 258 259 //首先要確定文件存在并且可寫 260 if (is_writable($file_path)) { 261 //使用添加模式打開$filename,文件指針將會在文件的開頭 262 if (!$handle = fopen($file_path, 'a')) { 263 echo "不能打開文件 $filename"; 264 exit; 265 } 266 267 //將$somecontent寫入到我們打開的文件中。 268 if (!fwrite($handle, $error_content)) { 269 echo "不能寫入到文件 $filename"; 270 exit; 271 } 272 273 //echo "文件 $filename 寫入成功"; 274 275 echo "——錯誤記錄被保存!"; 276 277 //關閉文件 278 fclose($handle); 279 } else { 280 echo "文件 $filename 不可寫"; 281 } 282 283 } else { 284 //首先要確定文件存在并且可寫 285 if (is_writable($file_path)) { 286 //使用添加模式打開$filename,文件指針將會在文件的開頭 287 if (!$handle = fopen($file_path, 'a')) { 288 echo "不能打開文件 $filename"; 289 exit; 290 } 291 292 //將$somecontent寫入到我們打開的文件中。 293 if (!fwrite($handle, $error_content)) { 294 echo "不能寫入到文件 $filename"; 295 exit; 296 } 297 298 //echo "文件 $filename 寫入成功"; 299 echo "——錯誤記錄被保存!"; 300 301 //關閉文件 302 fclose($handle); 303 } else { 304 echo "文件 $filename 不可寫"; 305 } 306 } 307 308 } 309 echo "<br />"; 310 if ($this->is_error) { 311 exit; 312 } 313 } 314 echo "</div>"; 315 echo "</fieldset>"; 316 317 echo "<br />"; 318 } 319 320 //釋放結果集 321 public function free() { 322 @ mysql_free_result($this->result); 323 } 324 325 //數據庫選擇 326 public function select_db($db_database) { 327 return mysql_select_db($db_database); 328 } 329 330 //查詢字段數量 331 public function num_fields($table_name) { 332 //return mysql_num_fields($this->result); 333 $this->query("select * from $table_name"); 334 echo "<br />"; 335 echo "字段數:" . $total = mysql_num_fields($this->result); 336 echo "<pre>"; 337 for ($i = 0; $i < $total; $i++) { 338 print_r(mysql_fetch_field($this->result, $i)); 339 } 340 echo "</pre>"; 341 echo "<br />"; 342 } 343 344 //取得 MySQL 服務器信息 345 public function mysql_server($num = '') { 346 switch ($num) { 347 case 1 : 348 return mysql_get_server_info(); //MySQL 服務器信息 349 break; 350 351 case 2 : 352 return mysql_get_host_info(); //取得 MySQL 主機信息 353 break; 354 355 case 3 : 356 return mysql_get_client_info(); //取得 MySQL 客戶端信息 357 break; 358 359 case 4 : 360 return mysql_get_proto_info(); //取得 MySQL 協議信息 361 break; 362 363 default : 364 return mysql_get_client_info(); //默認取得mysql版本信息 365 } 366 } 367 368 //析構函數,自動關閉數據庫,垃圾回收機制 369 public function __destruct() { 370 if (!empty ($this->result)) { 371 $this->free(); 372 } 373 mysql_close($this->conn); 374 } //function __destruct(); 375 376 /*獲得客戶端真實的IP地址*/ 377 function getip() { 378 if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) { 379 $ip = getenv("HTTP_CLIENT_IP"); 380 } else 381 if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) { 382 $ip = getenv("HTTP_X_FORWARDED_FOR"); 383 } else 384 if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) { 385 $ip = getenv("REMOTE_ADDR"); 386 } else 387 if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) { 388 $ip = $_SERVER['REMOTE_ADDR']; 389 } else { 390 $ip = "unknown"; 391 } 392 return ($ip); 393 } 394 function inject_check($sql_str) { //防止注入 395 $check = eregi('select|insert|update|delete|/'|///*|/*|/././/|/.//|union|into|load_file|outfile', $sql_str); 396 if ($check) { 397 echo "輸入非法注入內容!"; 398 exit (); 399 } else { 400 return $sql_str; 401 } 402 } 403 function checkurl() { //檢查來路 404 if (preg_replace("/https?:([^/://]+).*/i", "//1", $_SERVER['HTTP_REFERER']) !== preg_replace("/([^/:]+).*/", "//1", $_SERVER['HTTP_HOST'])) { 405 header("Location: http://www.kebeke.com"); 406 exit(); 407 } 408 } 409 410 } 411 ?>?分頁類
1 <?php 2 /* 3 * Created on 2007-6-8 4 * Programmer : Alan , Msn - haowubai@hotmail.com 5 * php100.com Develop a project PHP - MySQL - Apache 6 * Window - Preferences - PHPeclipse - PHP - Code Templates 7 */ 8 //為了避免重復包含文件而造成錯誤,加了判斷函數是否存在的條件: 9 $page = $_GET[page]; 10 if(!function_exists(pageft)){ 11 //定義函數pageft(),三個參數的含義為: 12 //$totle:信息總數; 13 //$displaypg:每頁顯示信息數,這里設置為默認是20; 14 //$url:分頁導航中的鏈接,除了加入不同的查詢信息“page”外的部分都與這個URL相同。 15 // 默認值本該設為本頁URL(即$_SERVER["REQUEST_URI"]),但設置默認值的右邊只能為常量,所以該默認值設為空字符串,在函數內部再設置為本頁URL。 16 function pageft($totle,$displaypg=20,$url=''){ 17 18 //定義幾個全局變量: 19 //$page:當前頁碼; 20 //$firstcount:(數據庫)查詢的起始項; 21 //$pagenav:頁面導航條代碼,函數內部并沒有將它輸出; 22 //$_SERVER:讀取本頁URL“$_SERVER["REQUEST_URI"]”所必須。 23 global $page,$firstcount,$pagenav,$_SERVER; 24 25 //為使函數外部可以訪問這里的“$displaypg”,將它也設為全局變量。注意一個變量重新定義為全局變量后,原值被覆蓋,所以這里給它重新賦值。 26 $GLOBALS["displaypg"]=$displaypg; 27 28 if(!$page) $page=1; 29 30 //如果$url使用默認,即空值,則賦值為本頁URL: 31 if(!$url){ $url=$_SERVER["REQUEST_URI"];} 32 33 //URL分析: 34 $parse_url=parse_url($url); 35 $url_query=$parse_url["query"]; //單獨取出URL的查詢字串 36 if($url_query){ 37 //因為URL中可能包含了頁碼信息,我們要把它去掉,以便加入新的頁碼信息。 38 //這里用到了正則表達式,請參考“PHP中的正規表達式” 39 $url_query=ereg_replace("(^|&)page=$page","",$url_query); 40 41 //將處理后的URL的查詢字串替換原來的URL的查詢字串: 42 $url=str_replace($parse_url["query"],$url_query,$url); 43 44 //在URL后加page查詢信息,但待賦值: 45 if($url_query) $url.="&page"; else $url.="page"; 46 }else { 47 $url.="?page"; 48 } 49 50 //頁碼計算: 51 $lastpg=ceil($totle/$displaypg); //最后頁,也是總頁數 52 $page=min($lastpg,$page); 53 $prepg=$page-1; //上一頁 54 $nextpg=($page==$lastpg ? 0 : $page+1); //下一頁 55 $firstcount=($page-1)*$displaypg; 56 57 //開始分頁導航條代碼: 58 $pagenav="顯示第 <B>".($totle?($firstcount+1):0)."</B>-<B>".min($firstcount+$displaypg,$totle)."</B> 條記錄,共 $totle 條記錄"; 59 60 //如果只有一頁則跳出函數: 61 if($lastpg<=1) return false; 62 63 $pagenav.=" <a href="$url=1" mce_href="$url=1">首頁</a> "; 64 if($prepg) $pagenav.=" <a href="$url=$prepg" mce_href="$url=$prepg">前頁</a> "; else $pagenav.=" 前頁 "; 65 if($nextpg) $pagenav.=" <a href="$url=$nextpg" mce_href="$url=$nextpg">后頁</a> "; else $pagenav.=" 后頁 "; 66 $pagenav.=" <a href="$url=$lastpg" mce_href="$url=$lastpg">尾頁</a> "; 67 68 //下拉跳轉列表,循環列出所有頁碼: 69 $pagenav.=" 到第 <select name='topage' size='1' οnchange='window.location=/"$url=/"+this.value'>/n"; 70 for($i=1;$i<=$lastpg;$i++){ 71 if($i==$page) $pagenav.="<option value='$i' selected>$i</option>/n"; 72 else $pagenav.="<option value='$i'>$i</option>/n"; 73 } 74 $pagenav.="</select> 頁,共 $lastpg 頁"; 75 } 76 } 77 ?>?
轉載于:https://www.cnblogs.com/miencun/p/4246336.html
總結
以上是生活随笔為你收集整理的PHP数据库操作分页类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BlueMind 3.0.17 发布,消
- 下一篇: 常用的两个批处理