if语句的错误部分正在执行

时间:2013-10-07 13:16:26

标签: php

$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,但不知道该改变什么呢?

2 个答案:

答案 0 :(得分:3)

更好的方法是将此作为条件:

if($STH->rowCount()>0){
    // do this if records are returned
} else {
    // do this if no records are returned
}

答案 1 :(得分:2)

尝试像这样替换

  if ($STH->rowCount() > 0) {

  } else {

  } 

http://php.net/manual/en/pdostatement.rowcount.php

相关问题