日期:2014-05-17 浏览次数:20446 次
//DatabaseManager.php <?php class DatabaseManager{ protected $host; protected $name; protected $user; protected $psw; protected $connection; protected $close_flag; public function __construct($connection,$close_flag){ $this->connection = $connection; $this->connection = $close_flag; } protected function db_open(){ if(empty($this->connection)){ $this->connection = mysql_connect($this->host,$this->user,$this->psw); if (!$this->connection) { $this->db_handle_error_connetion(); return false; } if (!mysql_select_db($this->name,$this->connection)) { $this->da_handle_select(); return false; } } } public function db_close(){ if($this->connection) mysql_close($this->connection); } protected function db_handle_error_connetion(){ echo 'Failed connetion'; } protected function db_handle_select(){ echo 'Failed access database!'; } } ?>
//StudentDetailsDataManager.php <?php require 'DatabaseManager.php'; class StudentDetailsDataManager extends DatabaseManager{ public function __construct($connection="",$close_flag=true){ parent::__construct($connection, $close_flag); $this->host = "localhost"; $this->user = "root"; $this->psw = "root"; $this->name = "test"; $this->db_open(); } public function getStudentInfo($ID,&$data){ //$query = "SELECT * FROM student WHERE ID ='$ID'"; $query = "select * from student where ID = '$ID'"; $result = mysql_query($query); //print_r($result); if (!$result) { echo "result is empty!!"; return false; } $data = mysql_fetch_array($result,MYSQL_ASSOC); mysql_free_result($result); } } ?>
<?php require_once 'StudentDetailsDataManager.php'; $stuDataManager = new StudentDetailsDataManager(); $ID = "DA123456"; $data=NULL; $stuDataManager->getStudentInfo($ID, $data); $stuDataManager->db_close(); echo $data["ID"]; ?>