MySQL数据库封装跟分页查询
<?php
//我用的数据库名是house
class DBDA
{public $host="localhost";
public $uid="root";
public $pwd="root";
public $dbname="house";
public function Query($sql,$type=1)
//两个参数,第二个参数代表默认值
{
$db=new mysqli($this->host,$this->uid,$this->pwd,$this->dbname);
$result=$db->query($sql);
if($type=="1")
//定义1类型的是查询,其它类型是增删改
{
return $result->fetch_all();
}
elsle
{
return $result;
}
}
}
?>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>地区代号</td>
<td>地区名称</td>
<td>父级代号</td>
</tr>
<?php
include("DBDA.class.php");
$db = new DBDA(); //调用封装的数据库
include("page.class.php");
//查总条数
$sz = "select count(*) from chinastates";
$az = $db->Query($sz);
//1.造对象
$page = new Page($az[0][0],10);
//2.将分页类的limit变量拼接在SQL语句后面
$sql = "select * from chinastates ".$page->limit;
$arr = $db->Query($sql);
foreach($arr as $v)
{
echo "<tr>
<td>{$v[0]}</td>
<td>{$v[1]}</td>
<td>{$v[2]}</td>
</tr>";
}
?>
</table>
<?php
//3.输出分页信息
echo $page->fpage(); //括号()里面可以根据需要显示的内容加入0,1,2,3,4,5,6,7 需要显示什么可以从引用的工具表中查找
//此处括号内可以根据需要,从0-7输入。例如echo $page->fpage(4,5,6);就只显示上一页 页数 下一页