dsio68964998 2009-07-13 19:03
浏览 34
已采纳

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

Can anyone explain what the following PHP Code does


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;
    }

Can the above be done in a simpler way , would it be wise to use an ORM ?

  • 写回答

2条回答 默认 最新

  • doutan5337 2009-07-13 19:12
    关注

    The first class method looks like it performs a MySQL query and adds a LIMIT clause for pagination. The second moves the current query onto the next record, while incrementing the pagination counters.

    In more detail, here's the first sample:

    • Exit the method if the query is empty or the database connection doesn't exist.
    • Free any existing query.
    • If the number of records per page and page number are set:
      • Add them to the LIMIT clause of the query.
      • And reset them to 0.
    • Otherwise if records per page is set:
      • Add it to the LIMIT clause of the query.
      • And reset them to 0.
    • Run the query.
    • Set the current row to 0.
    • Collect errors.
    • If the query failed halt with the error.
    • Return the query.

    And the second:

    • If the query is not set halt with an error.
    • Fetch row information as an array for the current row.
    • Increment the row number.
    • Catch any errors.
    • If the result isn't an array free/close the query.
    • Otherwise return the result set.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题