$STH = $DBH -> prepare( "select * from table where id = :id" );
$STH -> bindParam( ':id', $_SESSION['id'], PDO::PARAM_INT, 4 );
$STH -> execute();
$result = $STH -> fetch();
if( isset( $result["id"] ) ) {
// do this if records are returned
} else {
// do this if no records are returned
}
由于某种原因,即使没有返回任何记录,if
语句的第一部分也会执行,为什么会发生这种情况?我假设它是因为isset
,但不知道该改变什么呢?
答案 0 :(得分:3)
更好的方法是将此作为条件:
if($STH->rowCount()>0){
// do this if records are returned
} else {
// do this if no records are returned
}
答案 1 :(得分:2)