删除前的PDO rowCount()在查询键存在时返回null

时间:2013-10-18 11:53:57

标签: php mysql pdo

public function deleteComment( $id, $check_author = true ) {
        $q = $this -> database -> prepare( "SELECT `id`, `aid` FROM `addon_comments` WHERE `id` = :id" );
        $q -> execute( array( 'id' => $id ) );

        if( !$q -> rowCount( ) )
            return -1;

        $r      = $q -> fetch( );
        $uid    = $this -> registry -> getClass( 'Users' ) -> isLogged( );

        if( $check_author && $r[ 'aid' ] != $uid )
            return -2;


        $s = $this -> database -> prepare( "DELETE FROM `addon_comments` WHERE `id` = :id OR `parent` = :id" );
        $s -> execute( array( 'id' => $id ) );

        return 1;           
    }

函数返回给我-1,但行被查询删除..当我评论删除查询时,一切正常(函数返回1).. 我做错了什么?

1 个答案:

答案 0 :(得分:0)

如果成功选择了行,则下面的代码将返回true:

if( $q -> rowCount( ) )
    return -1;

我建议您是否要检查否定结果并返回-1,请使用:

if($q->rowCount() < 1)
    return -1;