因为要写一个smarty分页,所以昨天发布了一篇PHP的分页类,今天写的是一个数据库的连接和显示数据的类,代码如下:
<?php
class Mydb{private $mysqli; //保存mysqli对象
private $db; //保存mysqli需要连接的数据库
private $tab; //保存mysqli需要连接的数据表
function __construct($db,$tab){
$this->db = $db;
$this->tab = $tab;
$this->mysqli = new mysqli('localhost','root','123456',$db);
}
public function getcount(){ //该方法用于获取数据表里的数据总条数
$sql="select * from ".$this->tab;
$re=$this->mysqli->query($sql);
return $re->num_rows;
}
public function getcontent($pagesize,$offset){ //该方法用于获取分页后的数据信息
$this->mysqli->query("set names utf8");
$sql="select * from ".$this->tab." limit ".$offset.",".$pagesize;
if($re=$this->mysqli->query($sql)){
while($arr=$re->fetch_assoc()){
$user[]=$arr;
}
return $user;
}else{
return false;
}
}
}
?>