一個資料庫操作PHP類

WBOY
發布: 2016-07-25 08:44:27
原創
1014 人瀏覽過
  1. /*
  2. * Author 墨龍
  3. * Time 2010年12月2日15:50:35
  4. * Time 2010年2月2日15:50:35
  5. */
  6. */
  7. $db = new mysql($db_host,$db_user,$db_password,$db_table,$db_conn,$pre,$coding);
  8. class mysql{
  9. private $host ;
  10. private $db_user;
  11. private $db_password;
  12. private $db_table;
  13. private $db_conn; //資料庫連線識別碼;
  14. private $result;識別
  15. private $sql; //sql執行語句
  16. private $pre; //資料庫表前綴
  17. private $coding; //資料庫編碼,GBK,UTF8,gb2312
  18. function __construct ($db_host,$db_user,$db_password,$db_table,$db_conn,$pre,$coding){
  19. $this->db_host = $db_host;
  20. $this->db_user = $db_user;
  21. $this->db_password = $db_password;
  22. $this->db_table = $db_table;
  23. $this->db_conn = $db_conn;
  24. $this->pre = $pre;
  25. $this->coding = $coding;
  26. $this->connect();
  27. }
  28. function connect(){
  29. $this->db_conn = @mysql_connect($this->db_host,$this->db_user,$this->db_password) or die($this->show_error("資料庫連結錯誤,請檢查資料庫連結設定! "));
  30. if(!mysql_select_db($this->db_table,$this->db_conn)){
  31. echo "找不到資料表:".$this->db_table;
  32. }
  33. mysql_select_db($this->db_table,$this->db_conn);
  34. $this->query("SET NAMES $this->coding");
  35. }
  36. / *執行SQL語句的函數*/
  37. function query($sql){
  38. if(emptyempty($sql)){ $this->show_error("你的sql語句不能為空!");
  39. }else{
  40. $this->sql = $sql;
  41. }
  42. $result = mysql_query($this->sql,$this->db_conn);
  43. return $this->result = $result;
  44. }
  45. /*建立新增新的資料庫*/
  46. public function create_database($databasename){ $database=$base_name ;
  47. $sqlDatabase = 'create database '.$database;
  48. return $this->query($sqlDatabase);
  49. }
  50. // 根據select查詢結果計算結果集條數
  51. public function db_num_rows(){
  52. if($this->result==null){
  53. if($this->show_error){
  54. $this->show_error("sql語句錯誤! ");
  55. }
  56. }else{
  57. return mysql_num_rows($this->result);
  58. }
  59. }
  60. /*查詢服務器所有數據庫*/
  61. //將系統資料庫與使用者資料庫分開,更直覺的顯示? .$amount =$this->db_num_rows($rs);
  62. echo "
    ";
  63. $i=1;
  64. while($row = $this->fetch_array($rs) ){
  65. echo "$i $row[Database]";
  66. echo "
    ";
  67. $i ;
  68. }
  69. }
  70. //以陣列形式傳回主機中所有資料庫名稱
  71. public function databases()
  72. {
  73. $rsPtr=mysql_list_dbs($this->db_conn);
  74. $i=0;
  75. $cnt=mysql_rows ($rsPtr);
  76. while($i {
  77. $rs[]=mysql_db_name($rsPtr,$i);
  78. $i ;
  79. }
  80. }
  81. return print_r($rs);
  82. }
  83. /*查詢資料庫下所有的表格*/
  84. function show_tables($database_name){
  85. $this->query("show tables" );
  86. echo "現有資料庫:".$amount = $this->db_num_rows($rs);
  87. echo "
    ";
  88. $i=1;
  89. while( $row = $this->fetch_array($rs)){
  90. $columnName="Tables_in_".$database_name;
  91. echo "$i $row[$columnName]";
  92. echo "
    ";
  93. $i ;
  94. }
  95. }
  96. /*
  97. mysql_fetch_row() array $row[0],$row[1],$row[2]
  98. mysql_fetch_array() array $row[0] 或$row[id]
  99. mysql_fetch_assoc() array 用$row->content 欄位大小寫敏感
  100. mysql_fetch_object() object 用$rowid] [content] 欄位大小寫敏感
  101. */
  102. /*取得記錄集,取得陣列-索引與關聯,使用$row['content'] */
  103. public function fetch_array()
  104. {
  105. return @mysql_fetch_array($this->result);
  106. }
  107. //取得關聯陣列,使用$row['欄位名稱']
  108. public function fetch_ass()
  109. {
  110. return @mysql_fetch_assoc($this->result);
  111. }
  112. //取得數字索引陣列,使用$row[0],$row[1],$row[2]
  113. public function fetch_row()
  114. {
  115. return @mysql_fetch_row($this->result);
  116. }
  117. //取得物件陣列,使用$row->content
  118. public function fetch_Object()
  119. {
  120. return @mysql_fetch_object($this->result);
  121. }
  122. //簡化查詢
  123. $table = $this->fulltablename($table);
  124. $this->query("select * from $table");
  125. }
  126. public function select($table ,$columnName,$condition){
  127. $table = $this->fulltablename($table);
  128. if(emptyempty($columnName)){
  129. $columnName = "*";
  130. } }
  131. $this->query("SELECT $columnName FROM $table $condition");
  132. }
  133. //簡化的insert
  134. function insert($table,$arr){
  135. $table = $this->fulltablename($table);
  136. $sql = "INSERT INTO $table ";
  137. if(!is_array($arr)){
  138. $this->show_error( "請輸入參數數組!");
  139. }else{
  140. $k = "";
  141. $v = "";
  142. foreach($arr as $key => $value){
  143. $k . = "`$key`,";
  144. $v .= "'".$value."',";
  145. }
  146. }
  147. $sql = $sql." (".substr($k,0,-1).") VALUES (".substr($v,0,-1).")";
  148. $this->query($sql);
  149. }
  150. // 簡化更新
  151. function update($table,$arr,$where){
  152. $table = $this->fulltablename($table)
  153. $sql = "UPDATE $table SET ";
  154. if(!is_array($arr)){
  155. $this->show_error("請輸入備份參數!");
  156. }else{
  157. foreach($arr as $key => $value){
  158. $sql .= " `".$key."` = '".$value." ' ,";
  159. }
  160. }
  161. $sql = substr($sql,0,-1)." where ".$where;
  162. return $this->query($sql) ;
  163. }
  164. //簡化刪除
  165. function delete($table,$where = ""){
  166. $table = $this->fullttablename($table); emptyempty($where)){
  167. $this->show_error("條件不可能為空!"
  168. }else{
  169. $where = " where ".$where
  170. } $sql = "DELETE FROM $table ".$where
  171. //echo $sql;
  172. return $this->query($sql); }
  173. //取得第一步INSERT操作生產ID
  174. public function insert_id (){
  175. return mysql_insert_id()
  176. }
  177. //附加編號調整
  178. public function fulltablename($table){
  179. return $table = $this->pre.$ table
  180. }
  181. //列數
  182. public function num_fields($table){
  183. $table = $ this->fulltablename($table);
  184. $this->query ("select * from $table");
  185. echo "
    ";
  186. echo "列數:".$total = mysql_num_fields($this->result);
  187. echo "
    "; 
    登入後複製
  188. for ($i=0; $i print_r(mysql_fetch_field($this->結果,$i) );
  189. }
  190. echo " "
  191. echo "
    "; 🎜>
  192. //取得MySQL伺服器資訊
  193. public function mysql_server($num=''){
  194. switch ($num){
  195. case 1 :
  196. return mysql_get_server_info (); // MySQL 主機資訊
  197. break;
  198. case 2 :
  199. return mysql_get_host_info();
  200. case 3 :
  201. return mysql_get_client_info (); //取得MySQL客戶端資訊
  202. case 4 :
  203. return mysql_get_proto_info();
  204. 預設:
  205. return mysql_get_client_info(); //取得mysql版本資訊
  206. }
  207. }
  208. //分析函數、自動控制號設定、容器恢復機制
  209. /*public function __destruct( )
  210. {
  211. if(!empty($this->result)){
  212. $this->free();
  213. }
  214. mysql_close( $ this->$db_conn );
  215. }*/
  216. /*取得客戶的真實IP 位置*/
  217. function getip(){
  218. if(getenv("HTTP_CLIENT_IP" ) && strcasecmp (getenv("HTTP_CLIENT_IP) "), "未知"))
  219. {
  220. $ip = getenv("HTTP_CLIENT_IP");
  221. }
  222. else if (getenv("HTTP_X🎜> }
  223. else if (getenv("HTTP_X_FORWARD_FORca(c getenv("HTTP_X_FORWARDED_FOR") , "未知")){
  224. $ip = getenv("HTTP_X_FORWARDED_FOR");
  225. }
  226. else if (getenv("REMO_DDR" ), "未知") )
  227. {
  228. $ip = getenv("REMOTE_ADDR")
  229. }
  230. else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER[ 'REMOTE_ADDR' ] && strcasecmp($ _SERVER['REMOTE_ADDR'], "未知")){
  231. $ip = $_SERVER['REMOTE_ADDR'];
  232. }
  233. else{
  234. $ip = "未知"
  235. }
  236. return($ip);
  237. }
  238. function show_error($str){
  239. echo "alert ('".$str ." ');history.back(-1);";
  240. }
  241. }
  242. ?>
  243. 回傳
PHP

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板