任何人都可以解释以下 PHP 代码的作用
function query($query_string)
{
if ($query_string == "") {
return 0;
}
if (!$this->connect()) {
return 0;
};
if ($this->QueryID) {
$this->free_result();
}
if ($this->RecordsPerPage && $this->PageNumber) {
$query_string .= " LIMIT " . (($this->PageNumber - 1) * $this->RecordsPerPage) . ", " . $this->RecordsPerPage;
$this->RecordsPerPage = 0;
$this->PageNumber = 0;
} else if ($this->RecordsPerPage) {
$query_string .= " LIMIT " . $this->Offset . ", " . $this->RecordsPerPage;
$this->Offset = 0;
$this->RecordsPerPage = 0;
}
$this->QueryID = @mysql_query($query_string, $this->LinkID);
$this->Row = 0;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
if (!$this->QueryID) {
$this->halt("Invalid SQL: " . $query_string);
}
return $this->QueryID;
}
function next_record()
{
if (!$this->QueryID) {
$this->halt("next_record called with no query pending.");
return 0;
}
$this->Record = @mysql_fetch_array($this->QueryID);
$this->Row += 1;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
$stat = is_array($this->Record);
if (!$stat && $this->AutoFree) {
$this->free_result();
}
return $stat;
}
以上可以用更简单的方式完成吗,使用 ORM 是否明智?
最佳答案
第一种方法看起来像是执行 MySQL 查询并添加了分页的 LIMIT 子句。第二个将当前查询移动到下一条记录,同时增加分页计数器。
更详细地说,这是第一个示例:
- 如果查询为空或数据库连接不存在,则退出该方法。
- 释放任何现有查询。
- 如果设置了每页记录数和页码:
- 将它们添加到查询的 LIMIT 子句中。
- 并将它们重置为 0。
- 否则,如果设置了每页记录数:
- 将其添加到查询的 LIMIT 子句中。
- 并将它们重置为 0。
- 运行查询。
- 将当前行设置为 0。
- 收集错误。
- 如果查询失败并因错误而停止。
- 返回查询。
第二个:
- 如果查询未设置为因错误而停止。
- 获取行信息作为当前行的数组。
- 增加行号。
- 捕获任何错误。
- 如果结果不是数组,则释放/关闭查询。
- 否则返回结果集。
关于php - 任何人都可以解释以下 PHP 代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1121445/