2011-11-28 51 views
4

我有一个类Database和类User。我的问题是我想创建一个数据库的实例,并在我的课程中使用它。PHP:使用数据库类

User只是其中之一。我希望能够使用数据库对象从表中获取用户记录。我不想在每个类中创建一个数据库变量,因为我将使用会话。有解决方案吗?

class Database 
{ 
    /* 
    * Edit the following variables 
    */ 
    private $db_host = 'localhost';  // Database Host 
    private $db_user = 'root';   // Username 
    private $db_pass = '';   // Password 
    private $db_name = 'researchportal';   // Database 
    /* 
    * End edit 
    */ 

    private $con = false;    // Checks to see if the connection is active 
    private $result = array();   // Results that are returned from the query 

    /* 
    * Connects to the database, only one connection 
    * allowed 
    */ 
    public function connect() 
    { 
     if(!$this->con) 
     { 
      $myconn = @mysql_connect($this->db_host,$this->db_user,$this->db_pass); 
      if($myconn) 
      { 
       $seldb = @mysql_select_db($this->db_name,$myconn); 
       if($seldb) 
       { 
        $this->con = true; 
        return true; 
       } 
       else 
       { 
        return false; 
       } 
      } 
      else 
      { 
       return false; 
      } 
     } 
     else 
     { 
      return true; 
     } 
    } 

    /* 
    * Changes the new database, sets all current results 
    * to null 
    */ 
    public function setDatabase($name) 
    { 
     if($this->con) 
     { 
      if(@mysql_close()) 
      { 
       $this->con = false; 
       $this->results = null; 
       $this->db_name = $name; 
       $this->connect(); 
      } 
     } 

    } 

    /* 
    * Checks to see if the table exists when performing 
    * queries 
    */ 
    private function tableExists($table) 
    { 
     $tablesInDb = @mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"'); 
     if($tablesInDb) 
     { 
      if(mysql_num_rows($tablesInDb)==1) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
    } 

    /* 
    * Selects information from the database. 
    * Required: table (the name of the table) 
    * Optional: rows (the columns requested, separated by commas) 
    *   where (column = value as a string) 
    *   order (column DIRECTION as a string) 
    */ 
    public function select($table, $rows = '*', $where = null, $order = null) 
    { 
     $q = 'SELECT '.$rows.' FROM '.$table; 
     if($where != null) 
      $q .= ' WHERE '.$where; 
     if($order != null) 
      $q .= ' ORDER BY '.$order; 

     $query = @mysql_query($q); 
     if($query) 
     { 
      $this->numResults = mysql_num_rows($query); 
      for($i = 0; $i < $this->numResults; $i++) 
      { 
       $r = mysql_fetch_array($query); 
       $key = array_keys($r); 
       for($x = 0; $x < count($key); $x++) 
       { 
        // Sanitizes keys so only alphavalues are allowed 
        if(!is_int($key[$x])) 
        { 
         if(mysql_num_rows($query) > 1) 
          $this->result[$i][$key[$x]] = $r[$key[$x]]; 
         else if(mysql_num_rows($query) < 1) 
          $this->result = null; 
         else 
          $this->result[$key[$x]] = $r[$key[$x]]; 
        } 
       } 
      } 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    /* 
    * Insert values into the table 
    * Required: table (the name of the table) 
    *   values (the values to be inserted) 
    * Optional: rows (if values don't match the number of rows) 
    */ 
    public function insert($table,$values,$rows = null) 
    { 
     if($this->tableExists($table)) 
     { 
      $insert = 'INSERT INTO '.$table; 
      if($rows != null) 
      { 
       $insert .= ' ('.$rows.')'; 
      } 

      for($i = 0; $i < count($values); $i++) 
      { 
       if(is_string($values[$i])) 
        $values[$i] = '"'.$values[$i].'"'; 
      } 
      $values = implode(',',$values); 
      $insert .= ' VALUES ('.$values.')'; 

      $ins = @mysql_query($insert); 

      if($ins) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
    } 

    /* 
    * Deletes table or records where condition is true 
    * Required: table (the name of the table) 
    * Optional: where (condition [column = value]) 
    */ 
    public function delete($table,$where = null) 
    { 
     if($this->tableExists($table)) 
     { 
      if($where == null) 
      { 
       $delete = 'DELETE '.$table; 
      } 
      else 
      { 
       $delete = 'DELETE FROM '.$table.' WHERE '.$where; 
      } 
      $del = @mysql_query($delete); 

      if($del) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
     else 
     { 
      return false; 
     } 
    } 

    /* 
    * Updates the database with the values sent 
    * Required: table (the name of the table to be updated 
    *   rows (the rows/values in a key/value array 
    *   where (the row/condition in an array (row,condition)) 
    */ 
    public function update($table,$rows,$where) 
    { 
     if($this->tableExists($table)) 
     { 
      // Parse the where values 
      // even values (including 0) contain the where rows 
      // odd values contain the clauses for the row 
      for($i = 0; $i < count($where); $i++) 
      { 
       if($i%2 != 0) 
       { 
        if(is_string($where[$i])) 
        { 
         if(($i+1) != null) 
          $where[$i] = '"'.$where[$i].'" AND '; 
         else 
          $where[$i] = '"'.$where[$i].'"'; 
        } 
       } 
      } 
      $where = implode('',$where); 


      $update = 'UPDATE '.$table.' SET '; 
      $keys = array_keys($rows); 
      for($i = 0; $i < count($rows); $i++) 
      { 
       if(is_string($rows[$keys[$i]])) 
       { 
        $update .= $keys[$i].'="'.$rows[$keys[$i]].'"'; 
       } 
       else 
       { 
        $update .= $keys[$i].'='.$rows[$keys[$i]]; 
       } 

       // Parse to add commas 
       if($i != count($rows)-1) 
       { 
        $update .= ','; 
       } 
      } 
      $update .= ' WHERE '.$where; 
      $query = @mysql_query($update); 
      if($query) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
     else 
     { 
      return false; 
     } 
    } 

    /* 
    * Returns the result set 
    */ 
    public function getResult() 
    { 
     return $this->result; 
    } 
} 

类用户:

<?php 
require_once 'class.database.php'; 

    class User{ 
     public $usr_id; 
     public $usr_name; 
     public $usr_level; 
     public $last_access_login; 

     public function __construct($id) { 
      $this->usr_id = $id; 
      //$this->usr_name = $usr_name; 
      //$this->usr_level = $usr_level; 
      $this->last_access_login = date("F d Y H:i:s.",time()); 
     } 

     public function getUser() 
     { 
    // $db->select('login_users','*','user_id='.$this->usr_id.''); 
    // $res = $db->getResult(); 
    // print_r($res); 

     $this->usr_name = $res['username']; 
     $this->usr_level = $res['user_level']; 
     $this->last_access_login = date("F d Y H:i:s.",time()); 

     } 
    } 

    $a = new User(3); 
    $a->getUser(); 

    ?> 

回答

3

你会想要将$ database传递给你的用户构造函数,或者使用像MrJ那样的单例。

通过你的数据库对象作为参数是非常简单的,你已经知道了,但你可以很清楚的给别人:

private $myDatabase = NULL; 

public function __construct(Database $myDatabase){ 
    $this->myDatabase = $myDatabase; 
} 

然后在课堂上任何地方,你可以使用使用

$this->myDatabase->select(...); 
数据库

现在创建对象变得简单:

$myUser = new User($database); 

Obviousl Y,适应它的需求是:)


单身人士使用也很简单,创建数据库类中的静态方法:

private static $staticInstance = NULL; 

public static function getInstance(){ 
    if(Database::$staticInstance == NULL){ 
     Database::$staticInstance = new Database(); 
     Database::$staticInstance->connect(); 
    } 
    return Database::$staticInstance; 
} 

这将创建一个自动创建的实例函数只要您需要它,您的唯一数据库类就会返回并在您再次调用getInstance时返回单个实例。

若要使用此您的其他类的内只需拨打:

$mydb = Database::getInstance(); 

然后用你的数据库对象...

单身人士在许多类似的情况下真的很有用。

+3

请不要略过基本解释,因为SO的90%的流量来自搜索引擎。有人在寻找这个问题可能需要这些细节。 –

+1

没问题,你完全正确,加上 –

+1

'private static $ staticInstance = NULL;'not'private static $ staticInstance = NULL:'Typo – SupaOden

1

你可能会想使用一个单独的类,例如,http://www.ricocheting.com/code/php/mysql-database-class-wrapper-v3是一个我个人使用

更新

我不完全确定,但您可以包括您所做的事情:

$database = new Database(); 
$client = new User($id, $database); 

然后内class User,并__construct()

public function __construct($id, $database){ 
    $this->db = $database; 
    ... 
} 

那么你应该能够调用它像类用户中$this->db->...(虽然我也不太清楚该如何解释好,但是这是一个替代方案可能/应该工作)

0

你应该看看singleton pattern implementation。这样你就可以从任何你喜欢的地方使用你的数据库类/连接。

+0

这正是我所提到的 - 用单(虽然你确实给出了全名:)) – MrJ

+0

这是类似于facad类吗? – SupaOden

0

只需将对象作为构造函数参数传递给User类即可。

new User(id, $db); 
0

为什么不只是扩展数据库类?

class.database.php

class Database extends ReflectionClass { 

    protected $db; 
    protected $_dbhost = DB_HOST; 
    protected $_dbname = DB_NAME; 
    protected $_dbpass = DB_PASS; 
    protected $_dbuser = DB_USER; 
    protected $_dboptions = array(PDO::ATTR_PERSISTENT => false); 

    public function __construct() { 
    parent::__construct($this); 
    $dsn = "mysql:host=$this->_dbhost;dbname=$this->_dbname"; 
    try { 
     $this->db = new PDO($dsn, $this->_dbuser, $this->_dbpass, $this->_dboptions); 
     $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    } 
    catch(Exception $e) { 
     error_log(print_r($e->getMessage(),1).' '.__FILE__.' '.__LINE__,0); 
     exit('Database is currently unavailable.'); 
    } 
    } 

    public function __get($name) { 
    if ($this->hasProperty($name)) 
     return $this->$name; 
    return null; 
    } 

    public function __set($name, $value) { 
    if ($this->hasProperty($name)) 
     $this->$name = $value; 
    return null; 
    } 

    public function __destruct(){ 
    $this->db = NULL; 
    } 

    public function select($tbl,$rows='*',$where=null,$order=null) { 
    $q = 'SELECT '.$rows.' FROM '.$tbl; 
    if(isset($where)) { 
     $q .= ' WHERE '.$where; 
    } 
    if(isset($order)) { 
     $q .= ' ORDER BY '.$order; 
    } 
    $q .= ' LIMIT 1'; 

    $r = $this->db->prepare($q); 
    foreach($this->getProperties() as $i=>$p){ 
     if ($p->class===$this->name) { 
     $prop = $p->name; 
     $r->bindColumn($i+1,$this->$prop); 
     } 
    } 
    $r->execute(); 
    $n = $r->rowCount(); 
    if($n) { 
     $r->fetch(); 
    } 
    return $n; 
    } 

} // end 

class.users.php

class Users extends Database { 
    protected $_id; 
    protected $_name; 
    protected $_level; 

    public function __construct() { 
    parent::__construct(); 
    } 

    public function selectUser($id) { 
    $tbl = 'Users'; 
    $rows = 'user_id,user_name,user_level'; 
    $where = "user_id=$id"; 
    $this->select($tbl,$rows,$where); 
    } 

} // end 

的index.php

// config 
$C['DB_HOST'] = 'localhost'; 
$C['DB_NAME'] = 'db'; 
$C['DB_PASS'] = 'root'; 
$C['DB_USER'] = 'root'; 
foreach($C as $k=>$v) { 
    define($k,$v); 
} 

// include 
require_once 'class.database.php'; 
require_once 'class.users.php'; 

// select 
$u = new Users(); 
$u->selectUser(1); 
echo $u->_id.'<br>'; 
echo $u->_name.'<br>'; 
echo $u->_level.'<br>';