2011-03-04 44 views
0

我寫一個MySQL的包裝類:。PHP - MySQL的包裝類建議

一)取代當前的一個 B)使生活更輕鬆的自定義功能 C組術語)學習和提高

我正在尋找關於如何改進我的技術和編碼風格的建議,所以我將不勝感激您可以提供的任何輸入。

免責聲明: 我知道還有其他的數據庫抽象方法,如PDO但我想我自己寫的,由於以上原因

感謝, 李


測試呼叫

#!/usr/bin/php 
<?php 

include('mysql.class.php'); 

$_mysql = new mysqli_ls(array('debug_log' => TRUE, 'query_log' => TRUE)); 

if ($_mysql->connect('host','user','password','db') === FALSE) print_r($_mysql->get_error()); 
else print "#1 connected\n"; 

if ($_mysql->set_database('auth_tracker_test') === FALSE) print_r($_mysql->get_error()); 
else print "#1 database changed\n"; 

/// Execute standard query 
$sql = "SELECT * from user"; 

if ($_mysql->SQLQuery($sql) === TRUE) print "#1 SELECT Query worked\n"; 
else print print_r($_mysql->get_error()); 


#print_r($_mysql->getArray()); 
print_r($_mysql->getRow()); 

$_mysql->disconnect(); 

?> 






<?php 

class mysqli_ls 
{ 

/**************************************************************************/ 
/*    SETUP VARIABLES           */ 
/**************************************************************************/ 

private $E_OK = TRUE; 
private $E_ERROR = FALSE; 

private $db_host; 
private $db_user; 
private $db_port; 
private $db_name; 
private $db_pass; 


private $result; 
private $link = FALSE; 
private $errorArr = array(); 

private $config = array( // Location of exisiting 
          'config_load' => FALSE, 
          'config_path' => 'database.cfg.php', 

          // Record errors to a file 
          'debug_log'  => FALSE, 
          'debug_log_path' => '/tmp/mysql.debug.log', 

          // Record queries to a file 
          'query_log'  => FALSE, 
          'query_log_path' => '/tmp/mysql.debug.log'); 

private $fh_debug = FALSE; 
private $fh_query = FALSE; 
private $fh_config = FALSE; 

/**************************************************************************/ 
/*    MAGIC FUNCTIONS           */ 
/**************************************************************************/ 

public function __construct($config = '') 
{ 
    // Config vars 
    if (!empty($config) && is_array($config)) $this->set_config($config); 

    // Open file handles if logs are required 
    // Debug Log 
    if ($this->config['debug_log'] === TRUE) 
    { 
     if (! $this->fh_debug = fopen($this->config['debug_log_path'], 'a')) 
     { 
     $this->handle_error('#01A', 'could not open debug log'); 
     return $this->E_ERROR; 
     } 
    } 

    // Query Log 
    if ($this->config['query_log'] === TRUE) 
    { 
     if (! $this->fh_query = fopen($this->config['query_log_path'], 'a')) 
     { 
     $this->handle_error('#01B', 'could not open query log'); 
     return $this->E_ERROR; 
     } 
    } 

    // Check mysqli functions are available 
    if (!function_exists('mysqli_connect')) 
    { 
     $this->handle_error('#01C', 'mysqli not installed'); 
     return $this->E_ERROR; 
    } 

    return $this->E_OK; 
} 

public function __deconstruct() 
{ 
    if ($this->link) $this->disconnect(); 
    return $this->E_OK; 
} 

/**************************************************************************/ 
/*    CONNECTION MANAGEMENT          */ 
/**************************************************************************/ 

public function connect($db_host='', $db_user='', $db_pass='', $db_name, $db_port='3306') 
{ 
    if (empty($db_host) || empty($db_user) || empty($db_pass) || empty($db_name) || empty($db_port)) 
    { 
     $this->handle_error('#02A', 'Missing connection variables'); 
     return $this->E_ERROR; 
    } 

    $this->db_host = $db_host; 
    $this->db_user = $db_user; 
    $this->db_pass = $db_pass; 
    $this->db_name = $db_name; 
    $this->db_port = $db_port; 

    $this->link = @new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name, $this->db_port); 
    if (mysqli_connect_error($this->link)) 
    { 
     $this->handle_error(mysqli_connect_errno($this->link), mysqli_connect_error($this->link)); 
     return $this->E_ERROR; 
    } 

    return $this->E_OK; 
} 

public function disconnect() 
{ 
    if ($this->link) 
    { 
     if ($this->link->close() === TRUE) return $this->E_OK; 
     else 
     { 
     $this->handle_error($this->link->errno, $this->link->error); 
     return $this->E_ERROR; 
     }  
    } 

    $this->handle_error('#03A','no activate database connection'); 
    return $this->E_ERROR; 
} 

public function connect_existing() 
{ 
} 

public function set_database($database) 
{ 
    if ($this->link->select_db($database) === FALSE) 
    { 
     $this->handle_error($this->link->errno, $this->link->error); 
     return $this->E_ERROR; 
    } 

    $this->E_OK; 
} 

/**************************************************************************/ 
/*    SQL INTERFACE            */ 
/**************************************************************************/ 

public function insert() 
{ 
} 

public function update() 
{ 
} 

public function delete() 
{ 
} 

public function select() 
{ 
} 

public function query($sql) 
{ 
    // If the result set has cleaned up, do so before a new query 
    if ($this->result) $this->result->close(); 

    // Record query 
    if ($this->config['query_log'] === TRUE) $this->write_log('query', $sql); 

    if ($result = $this->link->query($sql)); 
    { 
     $this->result = $result; 
     return $this->E_OK; 
    } 

    // Clean up the result set 
    $result->close(); 

    // Query failed, handle error 
    $this->handle_error($this->link->errno, $this->link->error); 
    return $this->E_ERROR; 
} 

/**************************************************************************/ 
/*    RESULT FUNCTIONS           */ 
/**************************************************************************/ 

public function getArray($type = 'assoc') 
{ 
    switch($type) 
    { 
     case 'num': 
     $type = MYSQLI_NUM; 
     break; 

     case 'assoc': 
     $type = MYSQLI_ASSOC; 
     break; 

     case 'both': 
     $type = MYSQLI_BOTH; 
     break; 

     default: 
     $this->handle_error('#12A','invalid field type. Options are include num, assoc, both'); 
     return $this->E_ERROR; 
     break; 
    } 

    $resultArr = array(); 
    while($row = $this->result->fetch_array($type)) 
    { 
     $resultArr[] = $row; 
    } 

    return $resultArr; 
} 

public function getRow($type = 'assoc') 
{ 
    switch($type) 
    { 
     case 'num': 
     $type = MYSQLI_NUM; 
     break; 

     case 'assoc': 
     $type = MYSQLI_ASSOC; 
     break; 

     case 'both': 
     $type = MYSQLI_BOTH; 
     break; 

     default: 
     $this->handle_error('#13A','invalid field type. Options are include num, assoc, both'); 
     return $this->E_ERROR; 
     break; 
    } 

    return $this->result->fetch_array($type); 
} 

public function num_row() 
{ 
    return $this->result->num_rows; 
} 

public function insert_id() 
{ 
    return $this->link->insert_id; 
} 

public function affected_rows() 
{ 
    return $this->link->affected_rows; 
} 

/**************************************************************************/ 
/*    LEGACY SUPPORT            */ 
/**************************************************************************/ 

public function SQLQuery($sql='') 
{ 
    if (empty($sql)) 
    { 
     $this->handle_error('#19A','missing query string'); 
     return $this->E_ERROR; 
    } 

    // Check for a select statement 
    if (preg_match("/^select/i",$sql) === 0) 
    { 
     $this->handle_error('#19A','incorrect query type, SELECT expected'); 
     return $this->E_ERROR;   
    } 

    // Execute query 
    if ($this->query($sql) === $this->E_ERROR) return $this->E_ERROR; 

    // Return number of rows 
    return $this->num_row(); 
} 

public function SQLModify($sql='') 
{ 
    if (empty($sql)) 
    { 
     $this->handle_error('#19A','missing query string'); 
     return $this->E_ERROR; 
    } 

    // Execute query 
    if ($this->query($sql) === $this->E_ERROR) return $this->E_ERROR; 

    // Return affected rows 
    $this->affected_rows(); 
} 

public function numRow() 
{ 
    return $this->num_row(); 
} 

/**************************************************************************/ 
/*    LOGGING AND DEBUGGING          */ 
/**************************************************************************/ 

private function write_log($type, $msg) 
{ 
    $msg = date('Y-m-d H:i:s') ."\t". $type ."\t". $msg ."\n"; 

    switch($type) 
    { 
     case 'error': 
     fwrite($this->fh_debug, $msg); 
     break; 

     case 'query': 
     fwrite($this->fh_query, $msg); 
     break; 

     default: 
     return $this->E_ERROR; 
     break;  
    } 
} 

private function handle_error($errormsg, $errorno) 
{ 
    $this->errorArr[] = array('code' => $errorno, 
           'error' => $errormsg); 

    if ($this->config['debug_log'] === TRUE) 
    { 
     $msg = "($errorno) $errormsg";        
     $this->write_log('error', $msg); 
    } 

    return $this->E_OK; 
} 

public function get_error($type = 'string') 
{ 
    switch($string) 
    { 
     case 'string': 
     $error = end($this->errorArr); 
     return $error['error'] .' ('. $error['code'] .')'; 
     break; 

     case 'array': 
     return end($this->errorArr); 
     break; 
    } 

    return false; 
} 

/**************************************************************************/ 
/*    SET CONFIG VARS           */ 
/**************************************************************************/ 

public function set_config($config) 
{ 
    foreach ($config as $key => &$value) 
    { 
     if (! isset($this->config[$key])) 
     { 
     $this->handle_error('#19A','invalid field type'); 
     return $this->E_ERROR; 
     } 

     $this->config[$key] = $value; 
    } 

    return $this->E_OK; 
} 

/**************************************************************************/ 

} // Class END 

?> 

回答

1

我自己做了一次,並添加了另一個名爲MySqlTable的類,它代表了一個表。我回到它的__get功能,所以你可以把一個表

$sql->tablename->select(); 

下面是該__get函數的代碼:

function __GET($name) 
{ 
     return new MySqlTable($this, $name); 
} 

類是這樣的:

class MySqlTable 
{ 
    private $table; 
    private $mySql; 

    function MySqlTable(&$oMySql, $sTable) 
    { 
     $this->mySql = $oMySql; 
     $this->table = $sTable; 
    } 

    function &select($sWhere = '') 
    {   
     if (empty($sWhere)) 
     { 
     $data = $this->mySql->query("SELECT * FROM " . $this->table); 
     } 
     else 
     { 
     $data = $this->mySql->query("SELECT * FROM " . $this->table . " WHERE " . $sWhere); 
     } 

     return $this->mySql->resultToArray($data); 
    } 
} 
1

當前,您的mysqli_ls類包含查詢的結果。這使得在第二次查詢運行後不可能執行兩個查詢並使用第一個查詢的結果。

更好的方法是讓SQLQuery()方法返回一個結果對象,它包含結果句柄和從結果中檢索行的方法。

+0

謝謝爲此,我沒有考慮它。 – Lee 2011-03-04 13:38:01