-1

我的一个项目遇到了一些障碍。像许多书呆子一样,我决定创建自己的视频游戏评论网站。评论存储在数据库中,可以通过游戏标题检索到,网址如下:

http://www.example.com/reviews/ {gameName}/{可选pageOfReview}

不幸的是,在测试边缘情况时,我遇到了一个奇怪的错误——如果游戏的标题中有句点,我将无法检索它。如果句点是标题的主角(如 .hack),我会看到 Kohana 堆栈跟踪错误屏幕,告诉我评论(或更准确地说,游戏)不存在。如果它位于标题的中间或末尾,我会收到一条我自己的错误消息,指出无法检索评论(游戏)。有没有办法解决?这是MySQL如何解析句点的问题,还是其他问题?

编辑:所有查询都通过使用 MySQLi 驱动程序的 Kohana 2 的 ORM 功能处理。保存评论(管理员控制器):

public function saveReview()
{
    $this->checkAdmin();

    if (isset($_POST['submit'])) { $this->storeReview(); }
    else { header('Location: /admin'); }
}

private function storeReview($id = null)
{
    if (!preg_match("/^[a-zA-Z0-9\-_:!'. ]*$/", $_POST['gameTitle']) || empty($_POST['gameTitle'])) { $gameTitle = false; }
    else { $gameTitle = ucwords($this->clean($_POST['gameTitle'])); }

    if (!is_numeric($_POST['genre'])) { $genre = false; }
    else { $genre = $_POST['genre']; }

    $platformCheckArray = array_map('is_numeric', $_POST['platforms']);

    $platformCheck = true;
    foreach ($platformCheckArray as $pca)
    {
        if (!$pca)
        {
            $platformCheck = false;
            break;
        }
    }

    $proCheck = true;
    $cleanedPros = array();

    foreach ($_POST['pros'] as $pro)
    {
        if (!preg_match("/^[a-zA-Z0-9\-_:!' ]*$/", $pro))
        {
            $proCheck = false;
            break;
        }

        if (!empty($pro)) { $cleanedPros[] = $this->clean($pro); }
    }

    $conCheck = true;
    $cleanedCons = array();

    foreach ($_POST['cons'] as $con)
    {
        if (!preg_match("/^[a-zA-Z0-9\-_:!' ]*$/", $con))
        {
            $conCheck = false;
            break;
        }

        if (!empty($con)) { $cleanedCons[] = $this->clean($con); }
    }

    if (!is_numeric($_POST['score'])) { $score = false; }
    else { $score = $_POST['score']; }

    if (empty($_POST['content'])) { $content = false; }
    else { $content = true; }

    // save review if all tests pass, display error otherwise

    if ($gameTitle && $genre && $platformCheck && $proCheck && $conCheck && $score && $content)
    {
        $gameTitle = $gameTitle;
        $platforms = $_POST['platforms'];
        $reviewContent = $_POST['content'];
        $prosText = implode(', ', $cleanedPros);
        $consText = implode(', ', $cleanedCons);

        $game = ORM::factory('game');
        $game->title = $gameTitle;
        $game->genre_id = $genre;
        $game->platforms = $platforms;
        $game->save();

        $storedGenre = ORM::factory('genre')->where('id', $genre)->find();
        $storedGenre->platforms = $platforms;
        $storedGenre->save();

        $review = ORM::factory('review', $id);
        $review->content = $reviewContent;
        $review->score = $score;
        $review->game_id = $game->id;
        $review->date_added = date('Y-m-d H:i:s');
        $review->platforms = $platforms;
        $review->save();

        $pros = ORM::factory('pro');
        $pros->review_id = $review->id;
        $pros->text = $prosText;
        $pros->save();

        $cons = ORM::factory('con');
        $cons->review_id = $review->id;
        $cons->text = $consText;
        $cons->save();

        if ($game->saved && $storedGenre->saved && $review->saved && $pros->saved && $cons->saved) { $this->success('review'); }
        else { $this->showError("Something went wrong with saving the review.  Please try again."); }
    }
    else { $this->showError("All fields must contain values.  Please try again."); }
}

检索评论(从评论控制器):

public function show($id, $page = 1)
{
    if (is_numeric($id)) { $game = ORM::factory('game', $id); }
    else
    {
        $id = ucwords(stripslashes($id));
        $game = ORM::factory('game')->where('title', $id)->find();
    }

    if ($game->loaded) { $this->showReview($game->id, $page); }
    else { HandiError::factory('Could not retrieve the specified review.  Please check that you entered the correct value.'); }
}

private function showReview($id, $page = 1)
{
    $page = (int)$page;

    if ($page < 1) { $page = 1; }

    if ($id)
    {
        $game = ORM::factory('game', $id);
        $review = ORM::factory('review')->where('game_id', $game->id)->find();
        $genre = ORM::factory('genre')->where('id', $game->genre_id)->find();
        $revPlatforms = $this->db->query("SELECT * FROM platforms
                                      INNER JOIN platforms_reviews AS pr ON platforms.id = pr.platform_id 
                                      INNER JOIN reviews ON pr.review_id = reviews.id 
                                      WHERE reviews.id = ?", $review->id);
        $revPros = ORM::factory('pro')->where('review_id', $review->id)->find();
        $revCons = ORM::factory('con')->where('review_id', $review->id)->find();

        $platforms = array();
        foreach($revPlatforms as $rp) { $platforms[] = $rp->name; }
        $pros = explode(', ', $revPros->text);
        $cons = explode(', ', $revCons->text);

        $pages = explode('&lt;split /&gt;', $review->content);
        $count = count($pages);

        if ($page > ($count)) { $content = $pages[0]; }
        else { $content = $pages[$page - 1]; }

        $view = new View('reviews/show_review');
        $view->content = $content;
        $view->gameTitle = $game->title;
        $view->genre = $genre->name;
        $view->platforms = implode(', ', $platforms);
        $view->pros = $pros;
        $view->cons = $cons;
        $view->score = $review->score;
        $view->pages = $pages;
        $view->render(true);
    }
    else { HandiError::factory('Could not retrieve the specified review.  Please check that you entered the correct value.'); }
}

编辑2:嗯,我发现了一些关于领先时期的案例:

在我的控制器索引中,我有一些查询用于按游戏名称、平台、类型等列出评论。它基本上是一个穷人的维基。看:

public function index()
{
    /* show a wiki-like page with reviews listed by title, 
     * game title, genre, and platform
     */

    $numGenres = $this->db->query("SELECT COUNT(id) AS num FROM genres");
    $numPlatforms = $this->db->query("SELECT COUNT(id) AS num FROM platforms"); 

    $genreCount = $numGenres[0]->num;
    $platformCount = $numPlatforms[0]->num;
    $scoreCount = 5;

    $genreResults = array();
    $platformResults = array();
    $scoreResults = array();

    $gameResults = $this->db->query("SELECT LEFT(title, 1) AS letter, COUNT(id) AS count FROM games GROUP BY letter ORDER BY letter ASC");

    for($i = 1; $i < ($genreCount + 1); ++$i)
    {
        $genreResults[] = $this->db->query("SELECT genres.id AS id, genres.name AS name, COUNT(reviews.id) AS num FROM reviews 
                                            INNER JOIN games ON reviews.game_id = games.id 
                                            INNER JOIN genres ON games.genre_id = genres.id 
                                            WHERE genres.id = ?", $i);
    }

    for($j = 1; $j < ($platformCount + 1); ++$j)
    {
        $platformResults[] = $this->db->query("SELECT platforms.id AS id, platforms.name AS name, COUNT(reviews.id) AS num FROM reviews 
                                               INNER JOIN platforms_reviews AS pr ON reviews.id = pr.review_id 
                                               INNER JOIN platforms ON pr.platform_id = platforms.id 
                                               WHERE platforms.id = ?", $j);
    }

    for($k = 1; $k < ($scoreCount + 1); ++$k)
    {
        $scoreResults[] = $this->db->query("SELECT score, COUNT(id) AS num FROM reviews WHERE score = ?", $k);
    }

    $view = new View('reviews/index');
    $view->gamesByLetter = $gameResults;
    $view->genres = $genreResults;
    $view->platforms = $platformResults;
    $view->scores = $scoreResults;
    $view->render(true);
}

当我将这些查询的结果传递给视图时,我会遍历它们并根据元类别创建链接。因此,它显示了有多少游戏以字母 A、B 等开头,点击其中一个链接会将用户带到一个链接列表,每个链接都有评论(所以,A->Afterburner(等等) -> 审查加力燃烧器)。

当我将鼠标悬停在具有前导句点的组上时,我的状态栏显示链接中缺少句点,即使它显示在源中。因此,即使源将链接显示为 site.com/reviews/game/。浏览器将其显示为 site.com/reviews/game/ 这让我相信句点甚至没有被传递到方法中,并且堆栈跟踪似乎证实了(它声称缺少一个参数,这将是句点)。

编辑 3:好的,我查看了我的路线,但在那里找不到任何东西。也就是说,我确实有一个 .htaccess 文件,该文件 mod_rewrites 路由看起来很适合 SEO,所以我想知道这是否是问题所在。我自己从来没有写过 mod_rewrite 文件——Kohana 论坛上的人给了我这个,而且它有效,所以我就用它了。我可以理解一些涉及的正则表达式,但我的正则表达式 Fu 很弱。我相信最后一行具有“魔力”。

# Turn on URL rewriting
Options +FollowSymlinks
RewriteEngine On

# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is www.example.com/kohana/, use /kohana/
RewriteBase /

# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# For reuests that are not actual files or directories,
# Rewrite to index.php/URL

# Original rule:
# RewriteRule ^(.*)$ index.php/$1 [PT,L]

# Alternative rule:
# RewriteRule .* index.php/$0 [PT,L]

# 2nd alternative rule that works on ICDSoft:
RewriteRule .* index.php?kohana_uri=$0 [PT,QSA,L]

如果我没看错的话,'.' 仅表示任何单个字符。

可以一个'。除了表示文件扩展名或网络后缀(.com、.org 等)之外,还可以在格式良好的 URL 中使用吗?我的意思是,当我将鼠标悬停在与它们的链接上时,它们没有出现在 Firefox 的状态栏中,这让我相信这是浏览器/格式正确的问题,而不是编码问题。

4

4 回答 4

0

MySQL 对列数据中的句点没有问题。但是,句点用于分隔表名和列名:table.column. 如果您的查询未正确转义和引用,则句点可能会被错误地解释为表/列分隔符。

您如何准备查询?

于 2010-07-27T20:52:05.033 回答
0

我认为,这个问题出在 Kohana 框架中,而不是 SQL 中。从 url 检查过滤参数。尝试打印您的查询,看看它在执行时的样子,并观察您的经期发生了什么。

于 2010-07-27T20:53:51.000 回答
0

编辑:抱歉,我没有看到您使用的是 Kohana 2.x 版。我怀疑这是否适用于你。

只是一个猜测,但是您是否将路线设置为允许网址中的句点?默认情况下,Kohana 不允许使用句号。您需要将 Route::set() 的第三个参数设置为如下所示:

Route::set('reviews', 'reviews/<name>(/<page>)', array('name' => '[^/,;?]++', 'page' => '\d+')
    ->defaults(array(
        'controller' => 'reviews',
        'action'     => 'load',
        'name'       => NULL,
        'page'       => 1,
    ));

请参阅论坛帖子http://forum.kohanaframework.org/comments.php?DiscussionID=4320

于 2010-07-28T00:29:50.323 回答
0

是时候使用 Profiler 检查所有生成的查询了。

在 Controller::__construct() 放

新的探查器;

并找到可能损坏的查询。

其他可能的解决方案:通过您的代码,有时未关闭/未终止的数据库查询实例可能会破坏(或合并)其他查询......

于 2010-07-28T08:11:07.047 回答