一個資料庫操作PHP類

來源:互聯網
上載者:User
  1. /*
  2. * Author 墨龍
  3. * Time 2010年12月2日 15:50:35
  4. */
  5. $db = new mysql($db_host,$db_user,$db_password,$db_table,$db_conn,$pre,$coding);
  6. class mysql{
  7. private $db_host;
  8. private $db_user;
  9. private $db_password;
  10. private $db_table;
  11. private $db_conn; //資料庫連接標識;
  12. private $result; //執行query命令的結果資源標識
  13. private $sql; //sql執行語句
  14. private $pre; //資料庫表首碼
  15. private $coding; //資料庫編碼,GBK,UTF8,gb2312
  16. function __construct($db_host,$db_user,$db_password,$db_table,$db_conn,$pre,$coding){
  17. $this->db_host = $db_host;
  18. $this->db_user = $db_user;
  19. $this->db_password = $db_password;
  20. $this->db_table = $db_table;
  21. $this->db_conn = $db_conn;
  22. $this->pre = $pre;
  23. $this->coding = $coding;
  24. $this->connect();
  25. }
  26. function connect(){
  27. $this->db_conn = @mysql_connect($this->db_host,$this->db_user,$this->db_password) or die($this->show_error("資料庫連結錯誤,請檢查資料庫連結配置!"));
  28. if(!mysql_select_db($this->db_table,$this->db_conn)){
  29. echo "沒有找到資料表:".$this->db_table;
  30. }
  31. mysql_select_db($this->db_table,$this->db_conn);
  32. $this->query("SET NAMES $this->coding");
  33. }
  34. /*執行SQL語句的函數*/
  35. function query($sql){
  36. if(emptyempty($sql)){
  37. $this->show_error("你的sql語句不可為空!");
  38. }else{
  39. $this->sql = $sql;
  40. }
  41. $result = mysql_query($this->sql,$this->db_conn);
  42. return $this->result = $result;
  43. }
  44. /*建立添加新的資料庫*/
  45. public function create_database($database_name){
  46. $database=$database_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. //將系統資料庫與使用者資料庫分開,更直觀的顯示?
  62. public function show_databases(){
  63. $this->query("show databases");
  64. echo "現有資料庫:".$amount =$this->db_num_rows($rs);
  65. echo "
    ";
  66. $i=1;
  67. while($row = $this->fetch_array($rs)){
  68. echo "$i $row[Database]";
  69. echo "
    ";
  70. $i++;
  71. }
  72. }
  73. //以數組形式返回主機中所有資料庫名
  74. public function databases()
  75. {
  76. $rsPtr=mysql_list_dbs($this->db_conn);
  77. $i=0;
  78. $cnt=mysql_num_rows($rsPtr);
  79. while($i<$cnt)
  80. {
  81. $rs[]=mysql_db_name($rsPtr,$i);
  82. $i++;
  83. }
  84. return print_r($rs);
  85. }
  86. /*查詢資料庫下所有的表*/
  87. function show_tables($database_name){
  88. $this->query("show tables");
  89. echo "現有資料庫:".$amount = $this->db_num_rows($rs);
  90. echo "
    ";
  91. $i=1;
  92. while($row = $this->fetch_array($rs)){
  93. $columnName="Tables_in_".$database_name;
  94. echo "$i $row[$columnName]";
  95. echo "
    ";
  96. $i++;
  97. }
  98. }
  99. /*
  100. mysql_fetch_row() array $row[0],$row[1],$row[2]
  101. mysql_fetch_array() array $row[0] 或 $row[id]
  102. mysql_fetch_assoc() array 用$row->content 欄位大小寫敏感
  103. mysql_fetch_object() object 用$row[id],$row[content] 欄位大小寫敏感
  104. */
  105. /*取得記錄集,擷取數組-索引和關聯,使用$row['content'] */
  106. public function fetch_array()
  107. {
  108. return @mysql_fetch_array($this->result);
  109. }
  110. //擷取關聯陣列,使用$row['欄位名']
  111. public function fetch_ass()
  112. {
  113. return @mysql_fetch_assoc($this->result);
  114. }
  115. //擷取數字索引數組,使用$row[0],$row[1],$row[2]
  116. public function fetch_row()
  117. {
  118. return @mysql_fetch_row($this->result);
  119. }
  120. //擷取對象數組,使用$row->content
  121. public function fetch_Object()
  122. {
  123. return @mysql_fetch_object($this->result);
  124. }
  125. //簡化查詢select
  126. public function findall($table){
  127. $table = $this->fulltablename($table);
  128. $this->query("select * from $table");
  129. }
  130. public function select($table,$columnName,$condition){
  131. $table = $this->fulltablename($table);
  132. if(emptyempty($columnName)){
  133. $columnName = "*";
  134. }
  135. $this->query("SELECT $columnName FROM $table $condition");
  136. }
  137. //簡化的insert
  138. function insert($table,$arr){
  139. $table = $this->fulltablename($table);
  140. $sql = "INSERT INTO $table ";
  141. if(!is_array($arr)){
  142. $this->show_error("請輸入參數數組!");
  143. }else{
  144. $k = "";
  145. $v = "";
  146. foreach($arr as $key => $value){
  147. $k .= "`$key`,";
  148. $v .= "'".$value."',";
  149. }
  150. }
  151. $sql = $sql." (".substr($k,0,-1).") VALUES (".substr($v,0,-1).")";
  152. $this->query($sql);
  153. }
  154. //簡化的update
  155. function update($table,$arr,$where){
  156. $table = $this->fulltablename($table);
  157. $sql = "UPDATE $table SET ";
  158. if(!is_array($arr)){
  159. $this->show_error("請輸入參數數組!");
  160. }else{
  161. foreach($arr as $key => $value){
  162. $sql .= " `".$key."` = '".$value."' ,";
  163. }
  164. }
  165. $sql = substr($sql,0,-1)." where ".$where;
  166. return $this->query($sql);
  167. }
  168. //簡化的delete
  169. function delete($table,$where = ""){
  170. $table = $this->fulltablename($table);
  171. if(emptyempty($where)){
  172. $this->show_error("條件不可為空!");
  173. }else{
  174. $where = " where ".$where;
  175. }
  176. $sql = "DELETE FROM $table ".$where;
  177. //echo $sql;
  178. return $this->query($sql);
  179. }
  180. //取得上一步 INSERT 操作產生的 ID
  181. public function insert_id(){
  182. return mysql_insert_id();
  183. }
  184. //加上首碼的資料表
  185. public function fulltablename($table){
  186. return $table = $this->pre.$table;
  187. }
  188. //查詢欄位數量
  189. public function num_fields($table){
  190. $table = $this->fulltablename($table);
  191. $this->query("select * from $table");
  192. echo "
    ";
  193. echo "欄位數:".$total = mysql_num_fields($this->result);
  194. echo "
    ";  
  195. for ($i=0; $i<$total; $i++){
  196. print_r(mysql_fetch_field($this->result,$i) );
  197. }
  198. echo "
  199. ";
  200. echo "
    ";
  201. }
  202. //取得 MySQL 伺服器資訊
  203. public function mysql_server($num=''){
  204. switch ($num){
  205. case 1 :
  206. return mysql_get_server_info(); //MySQL 伺服器資訊
  207. break;
  208. case 2 :
  209. return mysql_get_host_info(); //取得 MySQL 主機資訊
  210. break;
  211. case 3 :
  212. return mysql_get_client_info(); //取得 MySQL 用戶端資訊
  213. break;
  214. case 4 :
  215. return mysql_get_proto_info(); //取得 MySQL 協議資訊
  216. break;
  217. default:
  218. return mysql_get_client_info(); //預設取得mysql版本資訊
  219. }
  220. }
  221. //解構函式,自動關閉資料庫,記憶體回收機制
  222. /*public function __destruct()
  223. {
  224. if(!empty($this->result)){
  225. $this->free();
  226. }
  227. mysql_close($this->$db_conn);
  228. }*/
  229. /*獲得用戶端真實的IP地址*/
  230. function getip(){
  231. if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
  232. {
  233. $ip = getenv("HTTP_CLIENT_IP");
  234. }
  235. else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){
  236. $ip = getenv("HTTP_X_FORWARDED_FOR");
  237. }
  238. else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
  239. {
  240. $ip = getenv("REMOTE_ADDR");
  241. }
  242. else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")){
  243. $ip = $_SERVER['REMOTE_ADDR'];
  244. }
  245. else{
  246. $ip = "unknown";
  247. }
  248. return($ip);
  249. }
  250. function show_error($str){
  251. echo "";
  252. }
  253. }
  254. ?>
複製代碼
PHP
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.