将字符串与$ _GET进行比较

时间:2012-03-15 22:31:37

标签: php get

我有12个不同的ID,我想与$_GET进行比较。现在我把我所有的id都拉出来了:

$sth = $dbh->query('SELECT DISTINCT courseId from training');  
$sth->setFetchMode(PDO::FETCH_ASSOC); 
    while($row = $sth->fetch('courseId')) {
        print_r($row['courseId'] . ',');
    }

返回:

8,5,9,10,12,7,4,3,2,6,11,1, 

现在,我想检查$_GET['courseId']

的ID

如何编写if语句来比较$row['courseId']$_GET['courseId']的值。我想做点什么:

if($_GET['courseId'] == $row['1']) {
    //Do something here
} elseif ($_GET['courseId'] == $row['2']) {
    //Do something different
}

希望这是有道理的。谢谢!

1 个答案:

答案 0 :(得分:1)

将您的print_r行替换为:

if( $row['courseId'] == $_GET['courseId']) break;

然后,循环之后:

if( !$row) echo "ERROR: Course ID not found";
else {
    // do stuff with the $row here.
}