php之dao层的封装和简单结果的展示

来源:互联网 发布:c语言新建文件步骤 编辑:程序博客网 时间:2024/06/05 03:50
<?php 



//链接数据库
class mysqlDB{
public $host;
public $port;
public $username;
public $password;
public $charset;
public $dbname;


//链接结果
private static $link;


private $resource;//资源


public static function getInstance($config){
if(!isset(self::$link)){
self::$link=new self($config);//用构造方法创建数据库连接对象
  }
return self::$link;


}
//构造函数----禁止new
 private  function __construct($config){
$this->host=isset($config['host'])?$config['host']:'localhost';
$this->port=isset($config['port'])?$config['port']:'80';
$this->username=isset($config['username'])?$config['username']:'root';
$this->password=isset($config['password'])?$config['password']:'';
$this->charset=isset($config['charset'])?$config['charset']:'utf8';
$this->dbname=isset($config['dbname'])?$config['dbname']:'lamboone';
//链接数据裤
$this->connect();
//设定链接编码
$this->setCharset($this->charset);
//选定数据库
$this->selectDB($this->dbname);
  }


//禁止克隆
  private function __clone(){}
//  这里进行链接
public function connect(){
//链接数据库得到资源
$this->resource=mysqli_connect("$this->host","$this->username","$this->password","$this->dbname","$this->port")or die("链接数据库失败");


  }


  public function setCharset($charset){
 //设置字符编码
  mysqli_query($this->resource,"set names utf8");
  }


  public function selectDB($dbname){
  //设置链接的数据库
  mysqli_query($this->resource,"use lamboone");
  }


//执行最基本的sql语句,如果失败直接结束,如果成功,执行结果;
public function query($sql){
if(!$result=mysqli_query($this->resource,$sql))
{
echo "失败的sql语句是".$sql;
echo "错误".mysqli_error();
echo "错误号".mysqli_errno();
die();
}
return $result;
}


//$sql字符串类型的select语句
public function getALL($sql){
$result=$this->query($sql);//调用的是上面的方法
$arr=array();//空数组,可以不声明,直接使用$arr[];
while($rec=mysqli_fetch_assoc($result)){
//遍历得到每一行数据
$arr[]=$rec;//数组添加到数组中,得到二维数组


}
return $arr;
}


//返回一行数据
public function getRow(){
$result=$this->qurey($sql);//调用的是上面的方法
if($rec=mysqli_fetch_assoc($result)){//返回的下表为字段名的一维数组
//得到一行数据
return $rec;


}
return false;
}


//返回一个数据(select语句的第一行第一列)
public function getOne(){
$result=$this->qurey($sql);//调用的是上面的方法
$rec=mysqli_fetch_row($result);//返回下表为数字的一维数组;且下表一定是0123.。。
if($rec===false){
return false;
}
return $rec[0];//数组第一项
}


}




?> 



===================查询结果用html表格展示===========

<?php   
header('Content-type:text/html;charset=utf-8');
//通过数据库操作,将比赛列表需要的数据库处理


//自动加载类
spl_autoload_register('useAutoload');
function useAutoload($calss_name){


require './resource.php';


}
//初始化mySqlDb 
$config=array(
'host'=>'localhost',
  'port'=>'3306',
  'username'=>'root',
  'password'=>'123',
  'charset'=>'utf8',
  'dbname'=>'lamboone',


);
$dao=mysqlDB::getInstance($config);//数据操作对象层


//数据库操作----修改数据
// $sql="update info set love='健身001' where name='lambro';";
// $sql="alter table info add  height int(10);";
// $sql="alter table info add grade int;";


// $sql="update info set height=100 where id=1;";
$sql="select *from info;";
$result =$dao->getALL($sql);
// var_dump($result);


?>


<!--利用html5展示数据-->
 <!DOCTYPE  html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>我的信息</title>


</head>
<body>
<table>

<tr>
<th>id</th>
<th>名字</th>
<th>爱好</th>
</tr>
<?php foreach($result as $row): ?>
<tr>
<td><?php echo $row['id'];  ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['love']; ?></td>
</tr>
<?php endForeach; ?>

</table>>


</body>


</html>