一个封装mysql函数的类

王朝mysql·作者佚名  2006-01-09
宽屏版  字体: |||超大  

最近开始学习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,写的代码不是很成熟,希望各们朋友能提供修改意见. 非常感谢!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有