1. 程式人生 > >PHP環境配置XAMPP,資料庫連線公共類

PHP環境配置XAMPP,資料庫連線公共類

(1)XAMPP下載地址:

https://www.apachefriends.org/download.html

(2)安裝各個模組(xdebug)

php配置php.ini檔案

apache配置apache.conf

(3)資料庫類

<?php

     class DB {
         /* 主機名稱 */
         public $db_host;
         /* 使用者名稱 */
         public $db_user;
         /* 密碼 */
         public $db_pwd;
         /* 資料庫名 */
         public $db_name;
         /* 資料庫連結名稱 */
         public $links;
         /* 構造方法屬性名稱和引數名稱一致但是表示含義不同 */
         function __construct($db_host, $db_user, $db_pwd, $db_name){
             $this -> db_host = db_host;
             $this -> db_user = db_user;
             $this -> db_pwd = db_pwd;
             $this -> db_name = db_name;
             /* 連結資料庫程式碼 */
             $this -> links = @mysql_connect($db_host, $db_user, $db_pwd) or die("資料庫連結失敗");
             /* echo $this -> links; */
             mysql_query("set names utf8");
             mysql_select_db($db_name,$this->links);
              
         }
         /* 執行增刪改查操作函式 */
         function query($sql){
             return mysql_query($sql);
         }
         /* 查詢記錄個數 */
         function numRows($sql){
             $result = $this -> query($sql);
             $count = mysql_num_rows($result);
             return $count;
         }
         /* 得到一條記錄的一維陣列 */
         function getOne($sql){
             $result = $this -> query($sql);
             $arr = mysql_fetch_assoc($result);
             return $arr;
         }
         /*得到多條記錄的二維陣列*/
         function getAll($sql){
             $result = $this -> query($sql);
             $rows = array();
             while($rs = mysql_fetch_assoc($result)){
                 $rows[] = $rs;
             }
             return $rows;
         }
         /* 解構函式 */
         function __destruct(){
             $this -> db_host = db_host;
             $this -> db_user = db_user;
             $this -> db_pwd = db_pwd;
             $this -> db_name = db_name;
         }
         /* 資料庫連線關閉函式 */
         function close_connect($links) {
             mysql_close($links);
         }
     }
      
    /* 建立資料庫連線 */
    //include 'DB.class.php';
    //$db = new DB('localhost', 'root', '', 'yudabo');
    //$db->close_connect($db->links);
      
?>