PHP+MySQL OOP 筆記

PHP+MySQL OOP 筆記

September 1, 2016

爬一下PHP+MySQL OOP雖然有人寫教學文,但似乎沒有 mysqli 函數的寫法,所以就手動寫了個。

<?php
define('DB_MYSQL_HOST', 'YOUR_DB_HOST_HERE');
define('DB_MYSQL_USER', 'YOUR_USERNAME');
define('DB_MYSQL_PW',   'YOUR_PASSWORD');
define('DB_MYSQL_DB',   'YOUR_DB_TABLE');
?>
<?php
require_once('defines.php');

/**************************
 * 資料庫登入格式化
 **************************/
class DBLogin{
  var $user;
  var $password;
  var $host;
  var $db;

  function __construct($host, $user, $password, $dbname)
  {
    $this->host     = $host;
    $this->user     = $user;
    $this->password = $password;
    $this->db       = $dbname;
  }

  public function the_user()
  {
    return StringSafe($this->user,0,255);
  }

  public function the_pw()
  {
    return $this->password;
  }

  public function the_host()
  {
    return $this->host;
  }

  public function the_db()
  {
    return $this->db;
  }
}

/**************************
 * MySQL連線
 **************************/
class DBMySQL{
  //設定變數
  var $_dbConn; //連線資料
  var $_qryRes; //儲存資料

  //連線MySQL
  // function connect($host,$user,$pw,$dbname){
  function __construct($hostname, $user, $password, $dbname){
    //建立連線
    $_mysql_conn = mysqli_connect($hostname,
                                  $user,
                                  $password,
                                  $dbname)
                                  or die(TXT_ERR_DB.' '.mysqli_connect_error());

    //設定UTF-8
    mysqli_set_charset($_mysql_conn, "utf8");

    $this -> _dbConn = $_mysql_conn;
    return true;
  }

  //關閉連線
  function __destruct()
  {
    mysqli_close($this->_dbConn);
  }

  //單一SQL查詢資料
  public function query($sql){
    //逃逸字元
    //$_sql = mysqli_real_escape_string($this->_dbConn, $sql);
    $_sql=$sql;

    //撈資料
    $qryData = mysqli_query($this->_dbConn, $_sql)
                or die(TXT_ERR_DB.' '.mysqli_errno($this->_dbConn));

    //儲存資料後回傳
    $this->_qryRes = $qryData;
    return $qryData;
  }

  //複數SQL查詢
  public function queryMulti($sql)
  {
    //逃逸字元
    //$_sql = mysqli_real_escape_string($this->_dbConn, $sql);
    $_sql=$sql;

    //撈資料
    if(mysqli_multi_query($this->_dbConn, $_sql)){
      do {
        if($_result = mysqli_store_result($this->_dbConn)){
          while ($_row = mysqli_fetch_all($_result, MYSQLI_ASSOC)) {
            $this->_qryRes[] = $_row;
          }
          mysqli_free_result($_result);
        }
      } while (mysqli_next_result($this->_dbConn));
    }

    return $this->_qryRes;
  }

  //回傳查詢值(全部)
  public function getAllData($type=null)
  {
    $_term  = $type;

    switch ($_term) {
      case 'MYSQLI_NUM':
        return mysqli_fetch_all($this->_qryRes, MYSQLI_NUM);
        break;

      case 'MYSQLI_ASSOC':
        return mysqli_fetch_all($this->_qryRes, MYSQLI_ASSOC);
        break;

      case 'MYSQLI_BOTH':
        return mysqli_fetch_all($this->_qryRes, MYSQLI_BOTH);
        break;

      default:
        return mysqli_fetch_all($this->_qryRes, MYSQLI_ASSOC);
        break;
    }
  }

  //回傳查詢值(逐筆)
  public function getData($type=null)
  {
    $_term  = $type;

    switch ($_term) {
      case 'MYSQLI_NUM':
        return mysqli_fetch_array($this->_qryRes, MYSQLI_NUM);
        break;

      case 'MYSQLI_ASSOC':
        return mysqli_fetch_array($this->_qryRes, MYSQLI_ASSOC);
        break;

      case 'MYSQLI_BOTH':
        return mysqli_fetch_array($this->_qryRes, MYSQLI_BOTH);
        break;

      default:
        return mysqli_fetch_array($this->_qryRes, MYSQLI_NUM);
        break;
    }
  }

  //回傳查詢值總數
  public function getRowsNum()
  {
    return mysqli_num_rows($this->_qryRes);
  }
}
?>

給我讚!

相關文章


© 2019-2021 Su Jing Jhong 版權所有 創用 CC 授權條款