-1
$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,但也不知道要改变什么?

4

2 回答 2

3

更好的方法是将其用作条件:

if($STH->rowCount()>0){
    // do this if records are returned
} else {
    // do this if no records are returned
}
于 2013-10-07T13:21:51.493 回答
2

尝试像这样替换

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

  } else {

  } 

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

于 2013-10-07T13:20:48.630 回答