Warning: count (): Parameter must be an array

0

I have the following error: Warning: count (): Parameter must be an array or an object that implements Countable in ... and I really do not know why you are giving it to me, the code is the following: '

protected static $conexion;

private static function getConexion(){
    self::$conexion = Conexion::conectar();
}

private static function desconectar(){
    self::$conexion = null;
}

/**
 * Metodo que sirve para validar el login
 * @parametro    objeto    $usuario=cedula
 * @return      booleano
 */
public static function login($cedula){
    $sql = "SELECT 
            id,nombre,apellido,cedula,correo,privilegio,fecha_registro 
            FROM usuarios WHERE cedula = :cedula AND pass = :pass";

    self::getConexion();

    $resultado = self::$conexion->prepare($sql);
    $resultado->bindValue(":cedula", $cedula->getCedula());
    $resultado->bindValue(":pass", $cedula->getPass());

    $resultado->execute();


    if (count($resultado)){
        return true;
    }

    return false;
}

And I'm trying to insert the following values

public static function login($cedula, $pass){
    $obj_usuario = new Usuario();
    $obj_usuario->setCedula($cedula);
    $obj_usuario->setPass($pass);

    UsuarioDao::login($obj_usuario);
}

I really do not know what I'm doing wrong if someone could help me or need more information I would give it, I need to solve this urgent problem

    
asked by Juan Osio 23.06.2018 в 23:13
source

1 answer

1

The problem is basically in this segment of your code:

if (count($resultado)){
        return true;
}

You are running the count function and it is only executed when you pass a parameter or object. Logically, you get an error because the SELECT of your database query is built so that only one record returns as a result:

$sql = "SELECT 
            id,nombre,apellido,cedula,correo,privilegio,fecha_registro 
            FROM usuarios WHERE cedula = :cedula AND pass = :pass";

As you can see in the previous segment of code that I extracted from your description, your query will always show only one result. I note that this statement also belongs to the login of your site / application so you may not / should change it so I would suggest you delete then the if that I mentioned at the beginning or adapt it to this particular query. For this you can use the following validation:

if ($resultado->rowCount()>0){
    return true;
}

All this if you must maintain the structure of that code intact because otherwise you should take into account the comment of @alomalbarez

I hope it helps. Greetings!

    
answered by 23.06.2018 / 23:37
source