php中一个可查询所有表的“通用”查询分页类
一个可查询所有表的“通用”查询分页类?
最近突发奇想,希望写出一个可以针对所有表的查询分页类。因为在实际的开发中,恐怕查询并将结果集分页显示是用得最多的代码,而表的结构是多样的,我想尽可能地提高代码的重用率和维护性。?
以下是我写的,请各位加以指点,测试,看能否进行更好的改进和更多的支持。?
目前还只支持单一的表,不支持联合查询。但未来可以会考虑如何支持。?
代码:?<?php /**************************************************************** 这个类主要是解决针对很多表的简单数据查询,还在完善中。希望广大网友多提意见和建议。 我的目的是开发出一个几乎可以适用于所有mysql表的查询并将结果集分页的类。 可以自动识别查询中要显示的字段值。 在此,特别声明,要感谢chinaunix的朋友,特别是PHP版的网友们长期以来给我的帮助。 特别要提的是NightKids,一直以来,他都给了我无私的帮助,甚至是他的源代码。 这个类可以被任何人自由引用,使用,修改。但请保留这段文字。 使用这个类造成的一切损失,都与作者tonera无关。 我还在考虑,对于一些复杂的联合查询,可以派生一个类,重新构造sql实现。 这个类没有考虑更多的显示风格,你可以自己构造。 *****************************************************************/ class browser{ var $c_table; //要查询的表名 var $c_rows; //要显示的行数 var $c_lation; //查询的条件 var $c_order; //排序的条件 var $c_result; //查询的数据连接句柄 var $c_query; //最终构造的查询 var $c_found; //结果集 var $c_error; //错误收集器 var $c_offset; //分页显示的偏移量 var $total; //结果集的总数 //连接数据库 function connect(){ include '../connect.inc.php'; if ($connection==false){ $this->c_error.="没有连接上数据库。"; exit; } $this->c_result=$connection; } //构造函数,初始化变量 function browser($tablename,$row,$sql,$lation,$orderby){ $this->c_table=$tablename; $this->c_rows=$row; if(empty($this->c_offset)){ $this->c_offset=0; } if (empty($tablename) or empty($row) or empty($sql)){ $this->c_error="没有查询的表或没有批定显示多少行或没有查询语句"; } $this->c_query=$sql; if (!empty($lation)){ $this->c_query.=" ".$lation; } if (!empty($orderby)){ $this->c_query.=" ".$orderby; } } //计算总页数 function TatolPage(){ $sult=mysql_query("select count(*) as 'total_rows' from $this->c_table",$this->c_result); if ($sult==false) { $this->c_error.="计算结果集总数目的查询失败,请检查。"; exit; } $tempvar=mysql_fetch_array($sult); $this->total=$tempvar[0]; } //查询得到结果集,存入数组c_found[][]中 function GetFound(){ $sult=mysql_query($this->c_query,$this->c_result) or die(mysql_error()); while ($found=mysql_fetch_array($sult)){ $this->c_found[]=$found; } } //查询数据,并将结果分页存入一个变量 function ShowTable(){ $this->connect(); $this->TatolPage(); if (empty($_GET[offset])){ $_GET[offset]=0; } $this->c_query.=" limit ".$_GET[offset].", ".$this->c_rows; $sult=mysql_query($this->c_query,$this->c_result) or die(mysql_error()); //解析query,得到要显示出来的字段值 $tempvar=explode(" ",$this->c_query); $fields=explode(",",$tempvar[1]); //字段值(数组) //显示数据到一个表 $echo_content.="
"; while([email protected]_fetch_array($sult)){ $echo_content.="
"; return $echo_content; } } /*例子 //browser("表名",每页显示的数目,"sql","查询条件","排序条件"); $gggg=new browser("news",5,"select auto_id,news_title from news","","order by newstime desc"); $temp=$gggg->ShowTable(); echo $temp; //$gggg->GetFound()是将查询结果集存在一个二维数组里,本例中没有用到。 */ ?>?
"; } //分页 if ($this->c_rows==0){ $this->c_error.="每页显示的数目不能为0"; exit; } $total_page=ceil($this->total/$this->c_rows); $pre_page=$_GET[offset]-$this->c_rows; //下一页 $nex_page=$_GET[offset]+$this->c_rows; //显示上一页 if ($pre_page>=0){ $echo_content.=""; $echo_content.="".$found[1]."
"; //显示用户指定的字段,此处需仔细看 for($i=2;$i
上页&"; }else{ $echo_content.=" "; }else{ $echo_content.="&下页"; } $echo_content.="上页&"; } //显示页码 for($i=1;$ic_rows==($i-1)){ $echo_content.="&第".$i."页&"; }else{ $echo_content.="&c_rows.">".$i."&"; } } //显示下一页 if ($nex_page!=0 and ($_GET[offset]+$this->c_rows)total){ $echo_content.="&下页

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.