php - 任何人都可以解释以下 PHP 代码吗?

标签 php mysql

任何人都可以解释以下 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/

相关文章:

mysql - 高效查询、表桥/索引和结构

mysql - 存储多选选项列表的数据类型是什么?

php - 禁止客户在 WooCommerce 结账时更改国家/地区

php - 在 Linux 上的内存缓存中存储 session 的问题

php - 如何使用 php 7 centos 6.5 安装 gd 库

mysql - 使用存储过程性能会提高

mysql - 命名表的最佳方式

php - mysql like 子句不适用于数组 php

php - 检查路径是否会因 open_basedir 而失败

php - MySql - 如何对两列中两个值的组合创建唯一约束