0

我正在尝试在我的 codeigniter 应用程序上调试一个问题,其中我的服务器返回 500 错误,因为当我尝试加载包含多个帖子的页面时它会永远挂起。

此错误是间歇性的。10 次中有 7 次,我的页面加载速度很快,CI 的分析器显示执行时间 < 0.6。

该模型使用这种结构,其中将根据发出的请求(即我的帖子、收藏夹、其他帖子)插入不同的 FROM 子句。没有 SQL 错误,没有 PHP 错误。只有处理的显着延迟和一些 Apache 错误,如

[Sat May 19 21:27:21 2012] [warn] mod_fcgid: process 21174 graceful kill fail, sending SIGKILL
[Sat May 19 21:27:27 2012] [notice] mod_fcgid: process /var/www/vhosts/example.com/httpdocs/blog/index.php(21174) exit(communication error), get stop signal 9

这种 SQL 模型是否容易出现无限循环或其他可能证明我的服务器发疯的问题?

class Home_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    function count_posts($author_id = NULL, $section = NULL, $user_id = NULL, $origin = NULL)
    {
        $clause = $this->clause($author_id, $section, $user_id, $origin);

        $query = $this->db->query("
        SELECT  *
        $clause
        ");

        return $query->num_rows;
    }

    function clause($author_id, $section, $user_id, $origin = NULL)
    {
        if ($section == 'favorites') {

            $clause  = "FROM post RIGHT JOIN favorites ON post_id_fk = post_id WHERE user_id_fk = $user_id";

        } elseif (($section == 'posts') AND ($origin == 'user')) {

            $clause = "FROM post WHERE post_author_id = $author_id";

        } else {

            if ($author_id == NULL) {

                $clause = "FROM post";

            } else {

                $clause = "FROM post WHERE post_author_id = $author_id";
            }
        }

        return $clause;
    }
}
4

1 回答 1

0

我猜在你看来你会做类似的事情

for ($i=0; $i<$this->count_posts(); $i++){
         // show posts
  }

?

您可以发布您的查看代码吗?

但是鉴于您可能正在做类似的事情 - 可以合理地假设模型中的 count_posts() 函数可能返回错误的数字,从而导致您的循环变得无限。

要调试它,请执行以下操作

//for ($i=0; $i<$this->count_posts(); $i++){
         // show posts
// }
echo $count_posts();

看看输出是否总是你所期望的......

于 2012-05-21T04:25:08.270 回答