PHP - MySQL包装器类建议

时间:2011-03-04 13:24:29

标签: php mysql class standards

我正在编写MySQL包装类:

a。)替换当前的 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

?>

2 个答案:

答案 0 :(得分:1)

目前,您的mysqli_ls类包含查询结果。这使得无法执行两个查询并在第二个查询运行后使用第一个查询的结果。

更好的方法是让SQLQuery()方法返回结果对象,其中包含结果句柄和从结果中检索行的方法。

答案 1 :(得分: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);
  }
}