mysql数据库封装和 分页查询

1 之前我们学到了php连接mysql数据库的增删改查,中间要多次调用数据库,

而且以后用到的表比较多,上传中如果需要改数据的话会非常麻烦,但是如果

我们把数据库封装,到时就可以很轻松的把改掉一些数据,使得php和数据库

正常连接

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

分页查询用到了 page.class.php 这个工具 帮助完成,非常轻松

原文地址:https://www.cnblogs.com/xiaodouding/p/6444800.html