检查类别问题

时间:2013-07-20 11:17:31

标签: php mysql

我遇到了SQL问题。例如,我有链接http://www.site.com/single_video.php?id=96&kategorija=fails

在该页面中有两列,一列用于显示视频,另一列用于相关视频。 但是,如果用户添加'链接,它将在列“相关视频”中显示任何内容。所以我试图过滤掉“有效”类别..但不知怎的,它不起作用.. 这是我的代码......

<?php
include 'includes/connect_video_index.php';
mysql_query("SET NAMES utf8"); //recimo bazi da postoje čak i hrvatska slova :D 
$result1 = mysql_query('SELECT * 
                          FROM video 
                         WHERE id!="'.$id1.'" 
                           AND kategorija="'.$escaped_cat.'" 
                      ORDER BY RAND() 
                         LIMIT 10', $con1);
while ($row1 = mysql_fetch_array($result1)){
    $kat = $_GET['kategorija'];
    $kategorija = mysql_real_escape_string($kat);
    if ($kategorija == $row1['kategorija']){
        echo 'This category exists'; //this part is working
    } else {
        //this part doesn't works when I enter blah blah, or whatever :S
        echo "This category doesnt' exist"; 
    }
}

1 个答案:

答案 0 :(得分:0)

如果键入'somethingThatCantBeFound',则找不到行,而while循环进行... 0迭代。这意味着您无法访问if语句。如果$escaped_cat$kategorija相同,我的意思是。是吗?

如果您想知道类别是否存在有视频,您可以这样做:

SELECT EXISTS (SELECT * FROM video WHERE kategorija = 'some category')