mysql 数据库封装类:返回索引、关联、字符串数组;分页查询封装类 :$page=new Page(表的总条数,每页的条数);$sql = "".$page->limit; echo $page->fpage();
<?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'> 共<b> {$this->total} </b>{$this->config["head"]} </span>";
$html[1] = " 本页 <b>".$this->disnum()."</b> 条 ";
$html[2] = " 本页从 <b>{$this->start()}-{$this->end()}</b> 条 ";
$html[3] = " <b>{$this->page}/{$this->pageNum}</b>页 ";
$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 = " <a href='{$this->uri}page=1'>{$this->config["first"]}</a> ";
$str .= "<a href='{$this->uri}page=".($this->page-1)."'>{$this->config["prev"]}</a> ";
return $str;
}
}
/* 在对象内部使用的私有方法,用于获取页数列表信息 */
private function pageList(){
$linkPage = " <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> ";
}
/*当前页的信息 */
if($this->pageNum > 1)
$linkPage .= "<span style='padding:1px 2px;background:#BBB;color:white'>{$this->page}</span> ";
/*当前页后面的列表 */
for($i=1; $i <= $inum; $i++){
$page = $this->page+$i;
if($page <= $this->pageNum)
$linkPage .= "<a href='{$this->uri}page={$page}'>{$page}</a> ";
else
break;
}
$linkPage .= '</b>';
return $linkPage;
}
/* 在对象内部使用的私有方法,获取下一页和尾页的操作信息 */
private function nextlast(){
if($this->page != $this->pageNum) {
$str = " <a href='{$this->uri}page=".($this->page+1)."'>{$this->config["next"]}</a> ";
$str .= " <a href='{$this->uri}page=".($this->pageNum)."'>{$this->config["last"]}</a> ";
return $str;
}
}
/* 在对象内部使用的私有方法,用于显示和处理表单跳转页面 */
private function goPage(){
if($this->pageNum > 1) {
return ' <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+\'\'"> ';
}
}
/* 在对象内部使用的私有方法,用于获取本页显示的记录条数 */
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();的更多相关文章
- MySQL数据库授权与索引
MySQL数据库授权与索引 目录 MySQL数据库授权与索引 一.数据库用户授权 1. 授予权限 2. 查看权限 3. 删除权限 4. 全部权限(all privileges) 二.MySQL索引 1 ...
- mysql数据库千万级别数据的查询优化和分页测试
原文地址:原创 mysql数据库千万级别数据的查询优化和分页测试作者:于堡舰 本文为本人最近利用几个小时才分析总结出的原创文章,希望大家转载,但是要注明出处 http://blog.sina.com. ...
- mysql数据库,当数据类型是float时,查询居然查询不出数据来
mysql数据库,当数据类型是float时,查询居然查询不出数据来,类似如下: 以后mysql数据库不用float类型,而double类型可以查得出来.
- T-SQL 随机返回特定行数据和分页查询
T-SQL 随机返回特定行数据和分页查询 T-SQL 语言相较于标准SQL添加了很多特性,为了提高SQL Server的表现,是有必要深入了解的,面试时一般也会包含这两个小问题. 首先,是在一个Adv ...
- MYSQL数据库学习六 索引的操作
6.1 索引 由于数据存储在数据库表中,所以索引是创建在数据库表对象上的,由表中的一个或多个字段生成的键组成,这些键存储在数据结构(B-树或哈希表)中,通过索引可以快速有效地查找与键值相关联的字段.根 ...
- MySQL数据库中的索引(一)——索引实现原理
今天我们来探讨一下数据库中一个很重要的概念:索引. MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构,即索引是一种数据结构. 我们知道,数据库查询是数据库的最主要 ...
- MySQL数据库篇之索引原理与慢查询优化之二
接上篇 7️⃣ 正确使用索引 一.索引未命中 并不是说我们创建了索引就一定会加快查询速度,若想利用索引达到预想的提高查询速度的效果, 我们在添加索引时,必须遵循以下问题: #1 范围问题,或者说条件 ...
- mysql数据库中的索引有那些、有什么用
本文主要讲述了如何加速动态网站的MySQL索引分析和优化. 一.什么是索引? 索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第 ...
- MySQL数据库中的索引(二)——索引的使用,最左前缀原则
上文中,我们了解了MySQL不同引擎下索引的实现原理,在本文我们将继续探讨一下索引的使用以及优化. 创建索引可以大大提高系统的性能. 第一,通过创建唯一性索引,可以保证数据库表中每一行数据的唯一性. ...
随机推荐
- ural 1018(树形dp)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17662 思路:典型的树形dp,处理的时候类似于分组背包,dp[i] ...
- ClassPathXmlApplicationContext的启动
Spring将ApplicationContext启动的全过程,refresh函数中包含了几乎ApplicationContext中提供的全部功能,而且此函数中逻辑非常清晰明了,很容易分析对应的层次及 ...
- Codeforces Round #293 (Div. 2)
A. Vitaly and Strings 题意:两个字符串s,t,是否存在满足:s < r < t 的r字符串 字符转处理:字典序排序 很巧妙的方法,因为s < t,只要找比t字典 ...
- 2015ACM/ICPC亚洲区长春站 B hdu 5528 Count a * b
Count a * b Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Tot ...
- 获取当前的时间,转化为char[]格式unix时间戳
/* 在这个程序当中实现获取当前的unix时间戳 转化为char[] */ #include<stdio.h> #include<stdlib.h> #include<t ...
- BZOJ3886 : [Usaco2015 Jan]Moovie Mooving
f[i]表示用i集合内的电影可以达到的最长时间 f[i]向f[i|(1<<j)]更新,此时的时间为第j部电影在f[i]前的最晚上映时间 先排序一遍离散化后用前缀最大值解决 时间复杂度$O( ...
- CSS HTML链接去掉小手与增添小手
style="cursor: hand" crosshair:精确定位“十”字形: text:文本“I”形: wait:等待,“沙漏”形: default:默认指针: help:帮 ...
- 【C语言】17-预处理指令3-文件包含
这讲介绍最后一个预处理指令---文件包含 一.基本概念 其实我们早就有接触文件包含这个指令了, 就是#include,它可以将一个文件的全部内容拷贝另一个文件中. 二.一般形式 1.第1种形式#inc ...
- salt执行报错一例
执行报错: 查看服务端日志: 认证有问题 重新认证吧!!! minion端: [root@super66 ~]# cd /etc/salt/[root@super66 salt]# lsminion ...
- php页面之间传值
echo("<script>window.open('2.php?head=".$head."');<script>");