一个可查询所有表的“通用”查询分页类 最近突发奇想,希望写出一个可以针对所有表的查询分页类。因为在实际的开发中,恐怕查询并将结果集分页显示是用得最多的代码,而表的结构是多样的,我想尽可能地提高代码的重用率和维护性。
以下是我写的,请各位加以指点,测试,看能否进行更好的改进和更多的支持。
目前还只支持单一的表,不支持联合查询。但未来可以会考虑如何支持。
代码:
/****************************************************************
这个类主要是解决针对很多表的简单数据查询,还在完善中。希望广大网友多提意见和建议。
我的目的是开发出一个几乎可以适用于所有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.="
"; $echo_content.="".$found[1]." | "; ".$found[$i]." | ";||||||||
上页&"; }else{ $echo_content.=" | |||||||||
上页&"; } //显示页码 for($i=1;$i if ($_GET[offset]/$this->c_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.="&下页 |
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()是将查询结果集存在一个二维数组里,本例中没有用到。
*/
?>

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.


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

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),

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version