一个封装mysql函数的类

类别:编程语言 点击:0 评论:0 推荐:

最近开始学习PHP 因为以前主要是在Asp.Net下写WEB应用程序,刚开始接触PHP的mysql函数的时候,感觉有些不习惯.试着按照ADO.Net的一些形式对mysql函数做了一个简单的封装.
目前主要有三个类mysqlclient_connection / mysqlclient_command / mysqlclient_dataReader:

mysqlclient_connection 负责与mysql server建立连接 mysqlclient_command 为该类提供mysqlclient_connection和SQL Query后,执行mysql_query函数. mysqlclient_dataReader 为该类提供mysql_query返回的结果集,读取结果集的数据和字段信息. 关于类的代码如下:

class mysqlclient_connection { private $status; private $handle; private $server; private $database; private $username; private $password; public function __construct($server,$database,$username,$password) { $status = "closed"; $handle = 0; $thi-->server = $server; $this->database = $database; $this->username = $username; $this->password = $password; } public function open() { $this->handle = mysql_connect($this->server,$this->username,$this->password) or die("connect mysql faild."); if($this->handle != 0) { //echo "$this->database"; mysql_select_db($this->database,$this->handle) or ("database ".$database." is not exists or reject visited"); } else { die("handle == 0"); } $this->status = "open"; return true; } public function close() { if($this->status == "open") { mysql_close($this->handle) or die("falid close this connection"); $this->status = "closed"; return true; } else { die("this connection is closed"); } } public function getHandle() { return $this->handle; } public function __destruct() { if($this->status == "open") { mysql_close($this->handle) or die("faild close this connection"); $this->status = "closed"; //return true; } } } class mysqlclient_command { private $commandString; private $activeConnection; public function __construct($commandString) { $this->commandString = $commandString; } public function setActiveConnection($connection) { $this->activeConnection = $connection; } public function executeDataReader() { if($this->activeConnection->getHandle() == 0) { die("this command does not have a active connection"); } //mysql_select_db("mxb",$this->activeConnection->getHandle()) or die("selected faild again"); $result = mysql_query($this->commandString,$this->activeConnection->getHandle()) or die($this->commandString . $this->activeConnection->getHandle().mysql_error()); return $result; } public function __destruct() { // } } class mysqlclient_dataReader { private $result; private $rowsCount; private $fieldsCount; private static $currentPos; public function __construct() { mysqlclient_dataReader::$currentPos = 0; } public function setResult($result) { $this->result = $result; $this->rowsCount = mysql_num_rows($this->result); $this->fieldsCount = mysql_num_fields($this->result); } public function nextRecord() { if(mysqlclient_dataReader::$currentPos < $this->rowsCount) { $tempRow = mysql_fetch_array($this->result); mysqlclient_dataReader::$currentPos++; return $tempRow; } else { return false; } } public function getFieldsRow() { for($i=0;$i<$this->fieldsCount;$i++) { $field = mysql_fetch_field($this->result,$i); $fieldsRow[] = $field; return $fieldsRow; } } public function __destruct() { // } }

我自己写了一个测试类的简单的php页 列出指定表中的数据 code:

$connection = new mysqlclient_connection(SERVER,DATABASE,USERNAME,PASSWORD); $command = new mysqlclient_command("select * from user"); //$comman-->activeConnection = $connection; $command->setActiveConnection($connection); $connection->open(); $result = $command->executeDataReader(); $reader = new mysqlclient_dataReader(); $reader->setResult($result); while($row = $reader->nextRecord()) { echo "".$row["id"]." : ".$row["name"]; } $connection->close(); ?> 因为刚开始接触PHP,写的代码不是很成熟,希望各们朋友能提供修改意见. 非常感谢!

本文地址:http://com.8s8s.com/it/it21852.htm