<?php
class czy
{
    public $host="localhost"; //地址
    public $uid="root";    //用户名
    public $pwd="*******";//用户密码
    public $dbname="*******";//用户数据库名
    /**
    *给一个sql语句,返回执行的结果
    *@param string @sql用户指定的sql语句
    *@param int $type 用户给的数据类型 0代表增删改 1代表查询
    *@return 返回查询的结果,如果是查询返回二维数组,如果是增删改则返回true或false
    */
    function Query($sql,$type=1)
    {
        //造连接对象
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);

        //执行sql语句
        $result = $db->query($sql);      //sql语句是查询,返回索引数组

        //从结果集对象里面去数据
        if($type==1)
        {
            return $result->fetch_all();
        }
        else
        {
            return $result;              //sql语句是增删改则返回true或false
        }
    }

    /**
    *给一个sql语句,返回关联的二维数组
    *@param string $sql 用户指定的sql语句
    *@param int $type 用户给的语句类型,0代表增删改,1代表查询
    *@return 返回查询的结果,如果是查询返回二维数组,如果是增涵盖则返回true或false
    */
    function UnionQuery($sql,$type=1)//返回关联数组
    {
        //造连接对象
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);

        //执行sql语句
        $result = $db->query($sql);            //sql语句是查询,返回索引数组

        //从结果集对象里面取数据
        if($type==1)
        {
            $attr = array();
            while($a = $result->fetch_assoc())          
            {
                $attr[]=$a;//把循环出的$a 放入$attr[]这个数组
            }

            return $attr;
        }
        else
        {
            return $result;
        }
    }

    /**
    *给一个sql语句,返回字符串
    *@param string $sql 用户指定的sql语句
    *@param int $type 用户给的语句类型,0代表增删改,1代表查询
    *@return  返回查询的结果,如果是查询返回字符串,如果是增删改返回true或false
    */
    function strQuery($sql,$type=1)
    {
        //造连接对象
        $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);

        //执行sql语句
        $result = $db->query($sql);

        //取数据
        if($type==1)
        {
            $attr = $result->fetch_all();
            $str="";
            foreach($attr as $v)
            {
                $str.=implode("^",$v);      //把一维数组$v中的值遍历出来再每个值中间加'^'符号分隔
                $str.="|";              //在每个$v之间加'|'符号分隔
            }
            return substr($str,0,strlen($str)-1);
        }
        else
        {
            return $result;
        }
    }
}
?>
<?php
    /**
        file: page.class.php
        完美分页类 Page
    */
    class Page {
        private $total;                            //数据表中总记录数
        private $listRows;                         //每页显示行数
        private $limit;                            //SQL语句使用limit从句,限制获取记录个数
        private $uri;                              //自动获取url的请求地址
        private $pageNum;                          //总页数
        private $page;                            //当前页
        private $config = array(
                'head' => "条记录",
                'prev' => "上一页",
                'next' => "下一页",
                'first'=> "首页",
                'last' => "末页"
            );
        //在分页信息中显示内容,可以自己通过set()方法设置
        private $listNum = 10;                     //默认分页列表显示的个数

        /**
            构造方法,可以设置分页类的属性
            @param    int    $total        计算分页的总记录数
            @param    int    $listRows    可选的,设置每页需要显示的记录数,默认为25条
            @param    mixed    $query    可选的,为向目标页面传递参数,可以是数组,也可以是查询字符串格式
            @param     bool    $ord    可选的,默认值为true, 页面从第一页开始显示,false则为最后一页
         */
        public function __construct($total, $listRows=25, $query="", $ord=true){
            $this->total = $total;
            $this->listRows = $listRows;
            $this->uri = $this->getUri($query);
            $this->pageNum = ceil($this->total / $this->listRows);
            /*以下判断用来设置当前面*/
            if(!empty($_GET["page"])) {
                $page = $_GET["page"];
            }else{
                if($ord)
                    $page = 1;
                else
                    $page = $this->pageNum;
            }

            if($total > 0) {
                if(preg_match('/\D/', $page) ){
                    $this->page = 1;
                }else{
                    $this->page = $page;
                }
            }else{
                $this->page = 0;
            }

            $this->limit = "LIMIT ".$this->setLimit();
        }

        /**
            用于设置显示分页的信息,可以进行连贯操作
            @param    string    $param    是成员属性数组config的下标
            @param    string    $value    用于设置config下标对应的元素值
            @return    object            返回本对象自己$this, 用于连惯操作
         */
        function set($param, $value){
            if(array_key_exists($param, $this->config)){
                $this->config[$param] = $value;
            }
            return $this;
        }

        /* 不是直接去调用,通过该方法,可以使用在对象外部直接获取私有成员属性limit和page的值 */
        function __get($args){
            if($args == "limit" || $args == "page")
                return $this->$args;
            else
                return null;
        }

        /**
            按指定的格式输出分页
            @param    int    0-7的数字分别作为参数,用于自定义输出分页结构和调整结构的顺序,默认输出全部结构
            @return    string    分页信息内容
         */
        function fpage(){
            $arr = func_get_args();

            $html[0] = "<span class='p1'>&nbsp;共<b> {$this->total} </b>{$this->config["head"]}&nbsp;</span>";
            $html[1] = "&nbsp;本页 <b>".$this->disnum()."</b> 条&nbsp;";
            $html[2] = "&nbsp;本页从 <b>{$this->start()}-{$this->end()}</b> 条&nbsp;";
            $html[3] = "&nbsp;<b>{$this->page}/{$this->pageNum}</b>页&nbsp;";
            $html[4] = $this->firstprev();
            $html[5] = $this->pageList();
            $html[6] = $this->nextlast();
            $html[7] = $this->goPage();

            $fpage = '<div style="font:12px \'\5B8B\4F53\',san-serif;">';
            if(count($arr) < 1)
                $arr = array(0, 1,2,3,4,5,6,7);

            for($i = 0; $i < count($arr); $i++)
                $fpage .= $html[$arr[$i]];

            $fpage .= '</div>';
            return $fpage;
        }

        /* 在对象内部使用的私有方法,*/
        private function setLimit(){
            if($this->page > 0)
                return ($this->page-1)*$this->listRows.", {$this->listRows}";
            else
                return 0;
        }

        /* 在对象内部使用的私有方法,用于自动获取访问的当前URL */
        private function getUri($query){
            $request_uri = $_SERVER["REQUEST_URI"];
            $url = strstr($request_uri,'?') ? $request_uri :  $request_uri.'?';

            if(is_array($query))
                $url .= http_build_query($query);
            else if($query != "")
                $url .= "&".trim($query, "?&");

            $arr = parse_url($url);

            if(isset($arr["query"])){
                parse_str($arr["query"], $arrs);
                unset($arrs["page"]);
                $url = $arr["path"].'?'.http_build_query($arrs);
            }

            if(strstr($url, '?')) {
                if(substr($url, -1)!='?')
                    $url = $url.'&';
            }else{
                $url = $url.'?';
            }

            return $url;
        }

        /* 在对象内部使用的私有方法,用于获取当前页开始的记录数 */
        private function start(){
            if($this->total == 0)
                return 0;
            else
                return ($this->page-1) * $this->listRows+1;
        }

        /* 在对象内部使用的私有方法,用于获取当前页结束的记录数 */
        private function end(){
            return min($this->page * $this->listRows, $this->total);
        }

        /* 在对象内部使用的私有方法,用于获取上一页和首页的操作信息 */
        private function firstprev(){
            if($this->page > 1) {
                $str = "&nbsp;<a href='{$this->uri}page=1'>{$this->config["first"]}</a>&nbsp;";
                $str .= "<a href='{$this->uri}page=".($this->page-1)."'>{$this->config["prev"]}</a>&nbsp;";
                return $str;
            }

        }

        /* 在对象内部使用的私有方法,用于获取页数列表信息 */
        private function pageList(){
            $linkPage = "&nbsp;<b>";

            $inum = floor($this->listNum/2);
            /*当前页前面的列表 */
            for($i = $inum; $i >= 1; $i--){
                $page = $this->page-$i;

                if($page >= 1)
                    $linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a>&nbsp;";
            }
            /*当前页的信息 */
            if($this->pageNum > 1)
                $linkPage .= "<span style='padding:1px 2px;background:#BBB;color:white'>{$this->page}</span>&nbsp;";

            /*当前页后面的列表 */
            for($i=1; $i <= $inum; $i++){
                $page = $this->page+$i;
                if($page <= $this->pageNum)
                    $linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a>&nbsp;";
                else
                    break;
            }
            $linkPage .= '</b>';
            return $linkPage;
        }

        /* 在对象内部使用的私有方法,获取下一页和尾页的操作信息 */
        private function nextlast(){
            if($this->page != $this->pageNum) {
                $str = "&nbsp;<a href='{$this->uri}page=".($this->page+1)."'>{$this->config["next"]}</a>&nbsp;";
                $str .= "&nbsp;<a href='{$this->uri}page=".($this->pageNum)."'>{$this->config["last"]}</a>&nbsp;";
                return $str;
            }
        }

        /* 在对象内部使用的私有方法,用于显示和处理表单跳转页面 */
        private function goPage(){
                if($this->pageNum > 1) {
                return '&nbsp;<input style="width:20px;height:17px !important;height:18px;border:1px solid #CCCCCC;" type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'page=\'+page+\'\'}" value="'.$this->page.'"><input style="cursor:pointer;width:25px;height:18px;border:1px solid #CCCCCC;" type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'page=\'+page+\'\'">&nbsp;';
            }
        }

        /* 在对象内部使用的私有方法,用于获取本页显示的记录条数 */
        private function disnum(){
            if($this->total > 0){
                return $this->end()-$this->start()+1;
            }else{
                return 0;
            }
        }
    }

mysql 数据库封装类:返回索引、关联、字符串数组;分页查询封装类 :$page=new Page(表的总条数,每页的条数);$sql = "".$page->limit; echo $page->fpage();的更多相关文章

  1. MySQL数据库授权与索引

    MySQL数据库授权与索引 目录 MySQL数据库授权与索引 一.数据库用户授权 1. 授予权限 2. 查看权限 3. 删除权限 4. 全部权限(all privileges) 二.MySQL索引 1 ...

  2. mysql数据库千万级别数据的查询优化和分页测试

    原文地址:原创 mysql数据库千万级别数据的查询优化和分页测试作者:于堡舰 本文为本人最近利用几个小时才分析总结出的原创文章,希望大家转载,但是要注明出处 http://blog.sina.com. ...

  3. mysql数据库,当数据类型是float时,查询居然查询不出数据来

    mysql数据库,当数据类型是float时,查询居然查询不出数据来,类似如下: 以后mysql数据库不用float类型,而double类型可以查得出来.

  4. T-SQL 随机返回特定行数据和分页查询

    T-SQL 随机返回特定行数据和分页查询 T-SQL 语言相较于标准SQL添加了很多特性,为了提高SQL Server的表现,是有必要深入了解的,面试时一般也会包含这两个小问题. 首先,是在一个Adv ...

  5. MYSQL数据库学习六 索引的操作

    6.1 索引 由于数据存储在数据库表中,所以索引是创建在数据库表对象上的,由表中的一个或多个字段生成的键组成,这些键存储在数据结构(B-树或哈希表)中,通过索引可以快速有效地查找与键值相关联的字段.根 ...

  6. MySQL数据库中的索引(一)——索引实现原理

    今天我们来探讨一下数据库中一个很重要的概念:索引. MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构,即索引是一种数据结构. 我们知道,数据库查询是数据库的最主要 ...

  7. MySQL数据库篇之索引原理与慢查询优化之二

    接上篇 7️⃣  正确使用索引 一.索引未命中 并不是说我们创建了索引就一定会加快查询速度,若想利用索引达到预想的提高查询速度的效果, 我们在添加索引时,必须遵循以下问题: #1 范围问题,或者说条件 ...

  8. mysql数据库中的索引有那些、有什么用

    本文主要讲述了如何加速动态网站的MySQL索引分析和优化. 一.什么是索引? 索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第 ...

  9. MySQL数据库中的索引(二)——索引的使用,最左前缀原则

    上文中,我们了解了MySQL不同引擎下索引的实现原理,在本文我们将继续探讨一下索引的使用以及优化. 创建索引可以大大提高系统的性能. 第一,通过创建唯一性索引,可以保证数据库表中每一行数据的唯一性. ...

随机推荐

  1. C#学习笔记(七)——定义类

    一.C#中类的定义 1.类的定义 class Myclass { } 默认情况下是类是内部的,即外部的项目是不可以访问这个类,相当于加internal来修饰.但实际上是不需要加入的. 如果要指定类是公 ...

  2. 在VMware Workstation上安装Kali Linux

    在VMware Workstation上安装Kali Linux VMware Workstation是一款功能强大的桌面虚拟计算机软件.该软件允许用户在单一的桌面上同时运行不同的操作系统,并且可以进 ...

  3. html5 header和group

    <html> <head> <meta charset="UTF-8" /> <meta name="generator&quo ...

  4. POJ2486 Apple Tree(树形DP)

    题目大概是一棵树,每个结点都有若干个苹果,求从结点1出发最多走k步最多能得到多少个苹果. 考虑到结点可以重复走,容易想到这么个状态: dp[u][k][0]表示在以结点u为根的子树中走k步且必须返回u ...

  5. 获取当前的时间,转化为char[]格式unix时间戳

    /* 在这个程序当中实现获取当前的unix时间戳 转化为char[] */ #include<stdio.h> #include<stdlib.h> #include<t ...

  6. Treap和名次树

    Treap名字的来源:Tree+Heap,正如名字一样,就是一颗简单的BST,一坨堆的合体.BST的不平衡的根本原因在于基于左<=根<=右的模式吃单调序列时候会无脑成长链,而Treap则添 ...

  7. HDU 1242 (BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...

  8. TYVJ P1048 田忌赛马 Label:dp

    描述     中国古代的历史故事“田忌赛马”是为大家所熟知的.话说齐王和田忌又要赛马了,他们各派出N匹马,每场比赛,输的一方将要给赢的一方200两黄金,如果是平局的话,双方都不必拿出钱.现在每匹马的速 ...

  9. 深入理解javascript中的立即执行函数(function(){…})()

    投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-06-12 我要评论 这篇文章主要介绍了深入理解javascript中的立即执行函数,立即执行函数也叫立即调用函数,通常它的写法是 ...

  10. 不要在init和dealloc函数中使用accessor

    不要在init和dealloc函数中使用accessor 文章目录 Objective-C 2.0 增加了 dot syntax,用于简单地调用成员变量的 accessor.相当于 java 的 ge ...