php操作mysql的類

來源:互聯網
上載者:User

[PHP]代碼

  1. class mysql {
  2. private $db_host; //資料庫主機
  3. private $db_user; //資料庫使用者名稱
  4. private $db_pwd; //資料庫使用者名稱密碼
  5. private $db_database; //資料庫名
  6. private $conn; //資料庫連接標識;
  7. private $result; //執行query命令的結果資源標識
  8. private $sql; //sql執行語句
  9. private $row; //返回的條目數
  10. private $coding; //資料庫編碼,GBK,UTF8,gb2312
  11. private $bulletin = true; //是否開啟錯誤記錄
  12. private $show_error = false; //測試階段,顯示所有錯誤,具有安全隱患,預設關閉
  13. private $is_error = false; //發現錯誤是否立即終止,預設true,建議不啟用,因為當有問題時使用者什麼也看不到是很苦惱的
  14. /*建構函式*/
  15. public function __construct($db_host, $db_user, $db_pwd, $db_database, $conn, $coding) {
  16. $this->db_host = $db_host;
  17. $this->db_user = $db_user;
  18. $this->db_pwd = $db_pwd;
  19. $this->db_database = $db_database;
  20. $this->conn = $conn;
  21. $this->coding = $coding;
  22. $this->connect();
  23. }
  24. /*資料庫連接*/
  25. public function connect() {
  26. if ($this->conn == "pconn") {
  27. //永久連結
  28. $this->conn = mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
  29. } else {
  30. //即使連結
  31. $this->conn = mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
  32. }
  33. if (!mysql_select_db($this->db_database, $this->conn)) {
  34. if ($this->show_error) {
  35. $this->show_error("資料庫不可用:", $this->db_database);
  36. }
  37. }
  38. mysql_query("SET NAMES $this->coding");
  39. }
  40. /*資料庫執行語句,可執行查詢添加修改刪除等任何sql語句*/
  41. public function query($sql) {
  42. if ($sql == "") {
  43. $this->show_error("SQL語句錯誤:", "SQL查詢語句為空白");
  44. }
  45. $this->sql = $sql;
  46. $result = mysql_query($this->sql, $this->conn);
  47. if (!$result) {
  48. //調試中使用,sql語句出錯時會自動列印出來
  49. if ($this->show_error) {
  50. $this->show_error("錯誤SQL語句:", $this->sql);
  51. }
  52. } else {
  53. $this->result = $result;
  54. }
  55. return $this->result;
  56. }
  57. /*建立添加新的資料庫*/
  58. public function create_database($database_name) {
  59. $database = $database_name;
  60. $sqlDatabase = 'create database ' . $database;
  61. $this->query($sqlDatabase);
  62. }
  63. /*查詢服務器所有資料庫*/
  64. //將系統資料庫與使用者資料庫分開,更直觀的顯示?
  65. public function show_databases() {
  66. $this->query("show databases");
  67. echo "現有資料庫:" . $amount = $this->db_num_rows($rs);
  68. echo "
    ";
  69. $i = 1;
  70. while ($row = $this->fetch_array($rs)) {
  71. echo "$i $row[Database]";
  72. echo "
    ";
  73. $i++;
  74. }
  75. }
  76. //以數組形式返回主機中所有資料庫名
  77. public function databases() {
  78. $rsPtr = mysql_list_dbs($this->conn);
  79. $i = 0;
  80. $cnt = mysql_num_rows($rsPtr);
  81. while ($i < $cnt) {
  82. $rs[] = mysql_db_name($rsPtr, $i);
  83. $i++;
  84. }
  85. return $rs;
  86. }
  87. /*查詢資料庫下所有的表*/
  88. public function show_tables($database_name) {
  89. $this->query("show tables");
  90. echo "現有資料庫:" . $amount = $this->db_num_rows($rs);
  91. echo "
    ";
  92. $i = 1;
  93. while ($row = $this->fetch_array($rs)) {
  94. $columnName = "Tables_in_" . $database_name;
  95. echo "$i $row[$columnName]";
  96. echo "
    ";
  97. $i++;
  98. }
  99. }
  100. /*
  101. mysql_fetch_row() array $row[0],$row[1],$row[2]
  102. mysql_fetch_array() array $row[0] 或 $row[id]
  103. mysql_fetch_assoc() array 用$row->content 欄位大小寫敏感
  104. mysql_fetch_object() object 用$row[id],$row[content] 欄位大小寫敏感
  105. */
  106. /*取得結果資料*/
  107. public function mysql_result_li() {
  108. return mysql_result($str);
  109. }
  110. /*取得記錄集,擷取數組-索引和關聯,使用$row['content'] */
  111. public function fetch_array($resultt="") {
  112. if($resultt<>""){
  113. return mysql_fetch_array($resultt);
  114. }else{
  115. return mysql_fetch_array($this->result);
  116. }
  117. }
  118. //擷取關聯陣列,使用$row['欄位名']
  119. public function fetch_assoc() {
  120. return mysql_fetch_assoc($this->result);
  121. }
  122. //擷取數字索引數組,使用$row[0],$row[1],$row[2]
  123. public function fetch_row() {
  124. return mysql_fetch_row($this->result);
  125. }
  126. //擷取對象數組,使用$row->content
  127. public function fetch_Object() {
  128. return mysql_fetch_object($this->result);
  129. }
  130. //簡化查詢select
  131. public function findall($table) {
  132. $this->query("SELECT * FROM $table");
  133. }
  134. //簡化查詢select
  135. public function select($table, $columnName = "*", $condition = '', $debug = '') {
  136. $condition = $condition ? ' Where ' . $condition : NULL;
  137. if ($debug) {
  138. echo "SELECT $columnName FROM $table $condition";
  139. } else {
  140. $this->query("SELECT $columnName FROM $table $condition");
  141. }
  142. }
  143. //簡化刪除del
  144. public function delete($table, $condition, $url = '') {
  145. if ($this->query("DELETE FROM $table WHERE $condition")) {
  146. if (!empty ($url))
  147. $this->Get_admin_msg($url, '刪除成功!');
  148. }
  149. }
  150. //簡化插入insert
  151. public function insert($table, $columnName, $value, $url = '') {
  152. if ($this->query("INSERT INTO $table ($columnName) VALUES ($value)")) {
  153. if (!empty ($url))
  154. $this->Get_admin_msg($url, '添加成功!');
  155. }
  156. }
  157. //簡化修改update
  158. public function update($table, $mod_content, $condition, $url = '') {
  159. //echo "UPDATE $table SET $mod_content WHERE $condition"; exit();
  160. if ($this->query("UPDATE $table SET $mod_content WHERE $condition")) {
  161. if (!empty ($url))
  162. $this->Get_admin_msg($url);
  163. }
  164. }
  165. /*取得上一步 INSERT 操作產生的 ID*/
  166. public function insert_id() {
  167. return mysql_insert_id();
  168. }
  169. //指向確定的一條資料記錄
  170. public function db_data_seek($id) {
  171. if ($id > 0) {
  172. $id = $id -1;
  173. }
  174. if (!@ mysql_data_seek($this->result, $id)) {
  175. $this->show_error("SQL語句有誤:", "指定的資料為空白");
  176. }
  177. return $this->result;
  178. }
  179. // 根據select查詢結果計算結果集條數
  180. public function db_num_rows() {
  181. if ($this->result == null) {
  182. if ($this->show_error) {
  183. $this->show_error("SQL語句錯誤", "暫時為空白,沒有任何內容!");
  184. }
  185. } else {
  186. return mysql_num_rows($this->result);
  187. }
  188. }
  189. // 根據insert,update,delete執行結果取得影響行數
  190. public function db_affected_rows() {
  191. return mysql_affected_rows();
  192. }
  193. //輸出顯示sql語句
  194. public function show_error($message = "", $sql = "") {
  195. if (!$sql) {
  196. echo "" . $message . "";
  197. echo "
    ";
  198. } else {
  199. echo "";
  200. echo "錯誤資訊提示:
    ";
  201. echo "";
  202. echo "";
  203. echo "錯誤號碼:12142";
  204. echo "
    ";
  205. echo "錯誤原因:" . mysql_error() . "

    ";
  206. echo "";
  207. echo "" . $message . "";
  208. echo "";
  209. echo "
    " . $sql . "
    ";
  210. $ip = $this->getip();
  211. if ($this->bulletin) {
  212. $time = date("Y-m-d H:i:s");
  213. $message = $message . "\r\n$this->sql" . "\r\n客戶IP:$ip" . "\r\n時間 :$time" . "\r\n\r\n";
  214. $server_date = date("Y-m-d");
  215. $filename = $server_date . ".txt";
  216. $file_path = "error/" . $filename;
  217. $error_content = $message;
  218. //$error_content="錯誤的資料庫,不可以連結";
  219. $file = "error"; //設定檔案儲存目錄
  220. //建立檔案夾
  221. if (!file_exists($file)) {
  222. if (!mkdir($file, 0777)) {
  223. //預設的 mode 是 0777,意味著最大可能的訪問權
  224. die("upload files directory does not exist and creation failed");
  225. }
  226. }
  227. //建立txt日期檔案
  228. if (!file_exists($file_path)) {
  229. //echo "建立日期檔案";
  230. fopen($file_path, "w+");
  231. //首先要確定檔案存在並且可寫
  232. if (is_writable($file_path)) {
  233. //使用添加模式開啟$filename,檔案指標將會在檔案的開頭
  234. if (!$handle = fopen($file_path, 'a')) {
  235. echo "不能開啟檔案 $filename";
  236. exit;
  237. }
  238. //將$somecontent寫入到我們開啟的檔案中。
  239. if (!fwrite($handle, $error_content)) {
  240. echo "不能寫入到檔案 $filename";
  241. exit;
  242. }
  243. //echo "檔案 $filename 寫入成功";
  244. echo "——錯誤記錄被儲存!";
  245. //關閉檔案
  246. fclose($handle);
  247. } else {
  248. echo "檔案 $filename 不可寫";
  249. }
  250. } else {
  251. //首先要確定檔案存在並且可寫
  252. if (is_writable($file_path)) {
  253. //使用添加模式開啟$filename,檔案指標將會在檔案的開頭
  254. if (!$handle = fopen($file_path, 'a')) {
  255. echo "不能開啟檔案 $filename";
  256. exit;
  257. }
  258. //將$somecontent寫入到我們開啟的檔案中。
  259. if (!fwrite($handle, $error_content)) {
  260. echo "不能寫入到檔案 $filename";
  261. exit;
  262. }
  263. //echo "檔案 $filename 寫入成功";
  264. echo "——錯誤記錄被儲存!";
  265. //關閉檔案
  266. fclose($handle);
  267. } else {
  268. echo "檔案 $filename 不可寫";
  269. }
  270. }
  271. }
  272. echo "
    ";
  273. if ($this->is_error) {
  274. exit;
  275. }
  276. }
  277. echo "";
  278. echo "
  279. ";
  280. echo "
    ";
  281. }
  282. //釋放結果集
  283. public function free() {
  284. @ mysql_free_result($this->result);
  285. }
  286. //資料庫選擇
  287. public function select_db($db_database) {
  288. return mysql_select_db($db_database);
  289. }
  290. //查詢欄位數量
  291. public function num_fields($table_name) {
  292. //return mysql_num_fields($this->result);
  293. $this->query("select * from $table_name");
  294. echo "
    ";
  295. echo "欄位數:" . $total = mysql_num_fields($this->result);
  296. echo "
    ";
  297. for ($i = 0; $i < $total; $i++) {
  298. print_r(mysql_fetch_field($this->result, $i));
  299. }
  300. echo "
  301. ";
  302. echo "
    ";
  303. }
  304. //取得 MySQL 伺服器資訊
  305. public function mysql_server($num = '') {
  306. switch ($num) {
  307. case 1 :
  308. return mysql_get_server_info(); //MySQL 伺服器資訊
  309. break;
  310. case 2 :
  311. return mysql_get_host_info(); //取得 MySQL 主機資訊
  312. break;
  313. case 3 :
  314. return mysql_get_client_info(); //取得 MySQL 用戶端資訊
  315. break;
  316. case 4 :
  317. return mysql_get_proto_info(); //取得 MySQL 協議資訊
  318. break;
  319. default :
  320. return mysql_get_client_info(); //預設取得mysql版本資訊
  321. }
  322. }
  323. //解構函式,自動關閉資料庫,記憶體回收機制
  324. public function __destruct() {
  325. if (!empty ($this->result)) {
  326. $this->free();
  327. }
  328. mysql_close($this->conn);
  329. } //function __destruct();
  330. /*獲得用戶端真實的IP地址*/
  331. function getip() {
  332. if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
  333. $ip = getenv("HTTP_CLIENT_IP");
  334. } else
  335. if (getenv("HTTP_X_FORWARDED_FOR") &&strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
  336. $ip = getenv("HTTP_X_FORWARDED_FOR");
  337. } else
  338. if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
  339. $ip = getenv("REMOTE_ADDR");
  340. } else
  341. if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] &&strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
  342. $ip = $_SERVER['REMOTE_ADDR'];
  343. } else {
  344. $ip = "unknown";
  345. }
  346. return ($ip);
  347. }
  348. function inject_check($sql_str) { //防止注入
  349. $check =eregi('select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile',$sql_str);
  350. if ($check) {
  351. echo "輸入非法注入內容!";
  352. exit ();
  353. } else {
  354. return $sql_str;
  355. }
  356. }
  357. function checkurl() { //檢查來路
  358. if (preg_replace("/https?:\/\/([^\:\/]+).*/i", "\\1", $_SERVER['HTTP_REFERER']) !== preg_replace("/([^\:]+).*/", "\\1", $_SERVER['HTTP_HOST'])) {
  359. header("Location: http://www.dareng.com");
  360. exit();
  361. }
  362. }
  363. }
  364. ?>
複製代碼
php, mysql
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.