现在的位置: 首页 > 综合 > 正文

PHP面向对象—-继承特性

2013年09月01日 ⁄ 综合 ⁄ 共 827字 ⁄ 字号 评论关闭

将数据库常用的操作(连接数据库,获得所有数据,获得一条记录,获得一列记录,获得一条索引数组,获得一条关联数组)都给封装到db.class.php里面,谁需要谁继承这个类
注意:提交封装的类文件

<?php
class db
{
private $hostname;
private $user;
private $pass;
private $dbname;
private $linkflag;
private $charset;

public function __construct($host,$user,$pass,$dbname,$charset)
{
$this->hostname=$host;
$this->user=$user;
$this->pass=$pass;
$this->dbname=$dbname;
$this->charset=$charset;
$this->linkflag=mysql_connect($this->hostname,$this->user,$this->pass);
mysql_select_db($this->dbname,$this->linkflag) or die('连接失败!');
mysql_query("set names ".$this->charset);
}

public function getAll($sql){

$result = mysql_query($sql);

$rows = array();

while($row=mysql_fetch_assoc($result)){

$rows[] = $row;

}

return $rows;

}

public function getOne($sql){

$result = mysql_query($sql);

$row = mysql_fetch_assoc($result);

return $row;

}

public function __destruct()
{
mysql_close($this->linkflag);
}

}

?>

抱歉!评论已关闭.