我有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']
如何编写if
语句来比较$row['courseId']
和$_GET['courseId']
的值。我想做点什么:
if($_GET['courseId'] == $row['1']) {
//Do something here
} elseif ($_GET['courseId'] == $row['2']) {
//Do something different
}
希望这是有道理的。谢谢!
答案 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.
}