一個php串連mysql資料庫的類

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

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

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