MySQL数据库封装和分页查询

1.数据库封装

<?php

//我用的数据库名是housedb

class DBDA

{public $host="localhost";
public $uid="root";
public $pwd="root";
public $dbname="housedb";

public function Query($sql,$type=1)
{
$db=new mysqli($this->host,$this->uid,$this->pwd,$this->dbname);

$result=$db->query($sql);
if($type=="1")
{
return $result->fetch_all();
}
elsle
{
return $result;
}
}


}

?>

 


 

 

2.分页查询

<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 需要显示什么可以从引用的工具表中查找

posted @ 2017-02-27 17:03  周全264  阅读(716)  评论(0编辑  收藏  举报